最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rounding a number up to get no decimal - Stack Overflow

programmeradmin1浏览0评论

I am writing a script (in J Script) that takes the pressure of Oil in a pump, and displays it on a digital display.

The problem is that the Pump outputs the pressure level in Pascals, while the digital display is intended to display in PSI (the number of digits on the display is limited to 4, and the pressure levels of the oil in the pump in pascals is like 15 digits.)

Currently the script is simple :

var Pump1_Digi : Demo3D.Visuals.BoxVisual = sender.FindChild("Pump1_Oil_Pressure_Digi");

Pump1_Digi.Pressure_Num = sender.Pump1_Oil_Pressure;
Pump1_Digi.PropertiesUpdated;

Pump1_Digi.Pressure_Num is the value I write to that is displayed on the digital display.

sender.Pump1_Oil_Pressure is the acutal value of the oil pressure in pascals.

I know that 6894.757 Pascals is 1 PSI

so I can do this:

var Pump1_Digi : Demo3D.Visuals.BoxVisual = sender.FindChild("Pump1_Oil_Pressure_Digi");
var Pump1toPSI : Pressure;

sender.Pump1_Oil_Pressure / 6894.757 = Pump1toPSI;

Pump1_Digi.Pressure_Num = Pump1toPSI
Pump1_Digi.PropertiesUpdated;

While my result is now in PSI, the numbers after the decimal point go on nearly for ever.

What I would like to do is just round the result to the nearest whole number.

Is there a parse function in Jscript to acplish this? Or does anyone know of a better way?

I am writing a script (in J Script) that takes the pressure of Oil in a pump, and displays it on a digital display.

The problem is that the Pump outputs the pressure level in Pascals, while the digital display is intended to display in PSI (the number of digits on the display is limited to 4, and the pressure levels of the oil in the pump in pascals is like 15 digits.)

Currently the script is simple :

var Pump1_Digi : Demo3D.Visuals.BoxVisual = sender.FindChild("Pump1_Oil_Pressure_Digi");

Pump1_Digi.Pressure_Num = sender.Pump1_Oil_Pressure;
Pump1_Digi.PropertiesUpdated;

Pump1_Digi.Pressure_Num is the value I write to that is displayed on the digital display.

sender.Pump1_Oil_Pressure is the acutal value of the oil pressure in pascals.

I know that 6894.757 Pascals is 1 PSI

so I can do this:

var Pump1_Digi : Demo3D.Visuals.BoxVisual = sender.FindChild("Pump1_Oil_Pressure_Digi");
var Pump1toPSI : Pressure;

sender.Pump1_Oil_Pressure / 6894.757 = Pump1toPSI;

Pump1_Digi.Pressure_Num = Pump1toPSI
Pump1_Digi.PropertiesUpdated;

While my result is now in PSI, the numbers after the decimal point go on nearly for ever.

What I would like to do is just round the result to the nearest whole number.

Is there a parse function in Jscript to acplish this? Or does anyone know of a better way?

Share asked Sep 15, 2011 at 20:19 Rob_IGSRob_IGS 5753 gold badges8 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You want the Math.ceil() function to round up to the nearest integer. Or Math.round() to round up or down as necessary:

Math.ceil(1.2098344305985700003482);
// 2

Math.round(1.2098344305985700003482);
// 1
发布评论

评论列表(0)

  1. 暂无评论