admin管理员组

文章数量:1302438

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

本文标签: javascriptRounding a number up to get no decimalStack Overflow