Javascript - Limit the number to no decimals [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm using following Javascript code in one of my calculations:
var bx1 = parseFloat(tx9) * ( 70 / 100 );
The output of bx1 is showing in decimals. However, I want it to be no decimals. Looking for some help!
Eg. bx1 = 700*70/100 = 489.999. I want it either 489 or 490, anything is fine.

Use Math.round().
var = Math.round(parseFloat(tx9) * ( 70 / 100 ));

You can use either Math.round(), Math.floor() or Math.ceil():
var bx1 = 489.999;
console.log(Math.floor(bx1));
console.log(Math.round(bx1));
console.log(Math.ceil(bx1));
floor() will round down your number.
round() will round your number (based on decimal value).
ceil() will round up your number.

Related

trying to round a number to nearest whole 100th number, Math.round only rounds after decimal [duplicate]

This question already has answers here:
Javascript: Round by 100 [duplicate]
(2 answers)
Closed 2 years ago.
In adobe javascript calculation field, I'm trying to round a whole number to nearest 100th, example:
round $2946.16 to $2900
and $2955.42 to $3000
var DP2 = Number(this.getField("DownPaymentBeforeTTL").valueAsString);
var SST = Number(this.getField("SubmissionSalesTax").valueAsString);
event.value = (Math.round(DP2+SST))
Math.round only rounds decimals in adobe javascript calculation text field.
You could divide the number by 100, round it to get an integer, and then multiply back by 100:
var total = DP2 + SST;
event.value = Math.round(total / 100) * 100;
A little cheat
Math.round((DP2+SST)/100)*100

not rounding tofixed [duplicate]

This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 5 years ago.
I'm trying to get the first decimals of a float number without any kind of rounding.
Example:
var myfloat = 1.1864526;
myfloat = myfloat.toFixed(2);
It returns 1.19 but I need 1.18.
I'm pretty sure there is an easy solution but I am unable to find it without converting the number to a string (not useful in this case).
Multiply the float value by 100, get the int value of the result then divide that int by 100. Something like this should work:
((int)(myFloat*100)) / 100

javascript math result lesser than 0 gets 0.0000000000 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why does javascript returns so many zeros and not just 0.24
And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round or .toFixed.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));

strange Javascript Float division result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
var coeff='0.1';
var amount='12.2';
var res = Math.floor(parseFloat(amount) / parseFloat(coeff));
console.log(res);
Why the result of this is 121 (I was expecting 122)?
EDIT: my question was ambiguous: no trouble with the floor function. I was just wondering why 12.2 / 0.1 is not equal to 122.
the result is 121.99999999999999
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
The Math.floor() function returns the largest integer less than or equal to a given number.
so it cuts off the .99999. you might want to use
Math.round(parseFloat(amount) / parseFloat(coeff));
instead

Strange result in modulo calculation [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
-224 / -37.333333333333336 = 6
-224 % -37.333333333333336 = -37.33333333333332
(-224 / -37.333333333333336) % 1 = 0
Why doesn't % return 0 if the first result is 6?
And why isn't the result a float in the third calculation?
All numbers in JavaScript are 64-bit floating point numbers. Just because it doesn't display the number as 0.0 doesn't mean it's not a double.
According to WolframAlpha:
-224 / -37.333333333333336 = 5.9999999999999995714285...
Due to accuracy limitations / rounding in floating point division, the division results in 6 in JavaScript.
The modulo operation, however, correctly sees that -37.3… doesn't quite fit into -224 six times. That means that the other 99.99…% of -37.33… is a leftover after the division, which corresponds to result of the modulo operation.
Hence, you get almost, but not quite, the divisor back.

Categories

Resources