492
Views
9
###### Round off decimal number 281.75 ,actual result I want 281.8
Question

Hi all, There is a distance value =281.75 but I want to round off as result =281.8.

After decimal I need only 1 digit in round off example value =281.75 and the actual result i want =281.8.

Kindly help us. I tried so many times.

281.75
result =281.8
I also tried that one also Trunc(Number) + If(Number=Trunc(Number),0,1) ,but not getting actual result it showing as result=282 only. Vignesh Prakash

you can  use javascript toFixed() to achieve Your requirement

check this fiddle

Thanks & Regards

Vignesh Prakash. Emman Si

Hello Yasmeen,

Have you tried using the built-in function Round? In your case you would define it as Round(value,1)

Hope this helps!

- Emman Yasmeen Shaikh

Emman Si wrote:

Hello Yasmeen,

Have you tried using the built-in function Round? In your case you would define it as Round(value,1)

Hope this helps!

- Emman

Yes I had already tried that built in math functionality for round number but not getting the actual result which i want as per required Yasmeen Shaikh

Hi Vignesh,

Hope this will work

Thanks !! Stuart Harris
Champion

Hi Yasmeen,

Also note that the Round action rounds to the nearest "even" integer in client and server actions (as per the documentation Emman linked to).

There is a MathUtils forge component that provides an additional action available for server actions Math_RoundAwayFromZero(n: decimal).  Although I notice it does not specify the number of decimal digits, but that will be updated shortly.

I hope this helps!

Kind regards,

Stuart Yasmeen Shaikh

Stuart Harris wrote:

Hi Yasmeen,

Also note that the Round action rounds to the nearest "even" integer in client and server actions (as per the documentation Emman linked to).

There is a MathUtils forge component that provides an additional action available for server actions Math_RoundAwayFromZero(n: decimal).  Although I notice it does not specify the number of decimal digits, but that will be updated shortly.

I hope this helps!

Kind regards,

Stuart

Thanks a lot Harris Kilian Hekhuis
MVP

Note that if you are creating a Reactive app, use the Math Utils Reactive component instead! delon best

It seems like toFixed()  is a better solution, but it is not! In some cases it will NOT round correctly. Also, Math.round() will NOT round correctly in some cases.

To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom JavaScript rounding function that performs a "nearly equal" test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

Number.prototype.roundTo = function(decimal) {

return +(Math.round(this + "e+" + decimal)  + "e-" + decimal);

}

var num = 9.7654;

console.log( num.roundTo(2)); //output 9.77 Kilian Hekhuis