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.75result =281.8I also tried that one also Trunc(Number) + If(Number=Trunc(Number),0,1) ,but not getting actual result it showing as result=282 only.
Hi Yasmeen Shaikh,
you can use javascript toFixed() to achieve Your requirement
http://jsfiddle.net/VsLp6/
check this fiddle
Thanks & Regards
Vignesh Prakash.
Hello Yasmeen,
Have you tried using the built-in function Round?
Here is a link of the doc: https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Logic/Built-in_Functions/Math#Round
In your case you would define it as Round(value,1)
Hope this helps!
- Emman
Emman Si wrote:
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
Hi Vignesh,
Hope this will work
Thanks !!
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
Stuart Harris wrote:
Thanks a lot Harris
Note that if you are creating a Reactive app, use the Math Utils Reactive component instead!
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
I would take a look at Math Utils Reactive (assuming you are building a Reactive app). It provides various rounding, flooring and truncing actions, that provide the correct mathematical way.