This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 5 years ago.
I'm given numbers in cents:
eg.
102042
982123
121212
I want to convert them to dollars. I figured the easiest way to do this is to convert the digits into a string and add a decimal.
eg.
1020.42
9821.23
1212.12
I'm currently doing this but it only rounds to 1 decimal. What would I need to do it make it round to 2 decimals?
var number = 102042
number.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
UPDATE: I found out what my issue was. It wasn't the rounding but the fact that I combined a lodash filter to a division incorrectly. Thanks again!
A cent is 1/100th of a dollar, so you can calculate the dollar amount by doing
dollars = number / 100
var number = 102042;
console.log(number/100);
The easiest way is to do it with the number itself, you can multiply by 0.01 or divide by 100 and you'll get that amount in dollars:
var numbers = [102042,982123,121212];
for(num of numbers)
console.log(num/100);
Related
This question already has an answer here:
Keep trailing or leading zeroes on number
(1 answer)
Closed 9 months ago.
I have some string values like "35.5" , "32.20" and I want them to be converted to numbers but keep the exact same decimals. When I use Number("32.0") for example I get 32 but I want 32.0. If I convert Number("35.5") I want 35.5 not 35.50, is there any way to do this easily?
if you want a fixed number of floating point you can use toFixed but be aware that returns a string
const strings = [ "35.5" , "32.20", "32.0"]
const result = strings.map(n => parseFloat(n).toFixed(1))
console.log(result)
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 1 year ago.
In my project, when I am subtracting 96.74 from 60, javascript is giving me 36.739999999999995 , but I need 36.74 to do further calculation.
What should I do, how do I overcome this problem??
Thanks in advance.
Use parseFloat and set the decimal places to 2 with .toFixed(2)
console.log(parseFloat(36.739999999999995).toFixed(2))
If you want to get rid of trailing zeroes cast the result to a Number:
var num = Number(parseFloat(36.7).toFixed(2));
console.log(num);
Example how to round to two decimals, if that was what you wanted?
var x = 36.739999999999995;
x = Math.round(x*Math.pow(10,2))/Math.pow(10,2);
console.log(x);
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
I have a fiddle here: http://jsfiddle.net/94wQJ/1/ - but probably someone can advise just by looking below.
<button type="button" id="allocate">Calc</button>
$('#allocate').click(function () {
val1 = 25.00;
val2 = 16.37;
val3 = val1-val2;
alert(val3);
});
25 - 16.37 = 8.63 - however, the alert for val3 = 8.62999999999
Why is it not accurate?
Thank you,
Mark
Try Using toFixed more information here
val3.toFixed(2)
Demo Fiddle
Why is it not accurate?
This is a duplicate of Is floating point math broken?, but to answer your specific question: floating point numbers generally store the number in base 2 because it allows storing more numbers more accurately than using base 10, at the expense of not being able to exactly store all base 10 numbers even with a small number of decimal places.
Many decimal fractions not be represented exactly in binary.
Use .toFixed(2)
Please check this
val3 = Math.round(val3*100)/100;
I have update here http://jsfiddle.net/94wQJ/7/
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 8 years ago.
Tried :
var a=10.3;
var b=2.3;
alert(a+b);
but I get 12.600000000000001. I know JavaScript is loosely typed, but I hope I can do a sum :)
you can use toFixed() method also
var a=10.3;
var b=2.3;
alert((a+b).toFixed(1));
Works in chrome
Multiply to the precision you want then round and divide by whatever you multiplied by:
var a=10.3;
var b=2.3;
alert(Math.round((a+b) * 10) / 10);
http://jsfiddle.net/DYKJB/3/
It's not about the typing but about the precision of floating point types. You need to round for presentation.
Floating point types are not a good choice if you need exact values. If you want to express currency values express them as cents or use an appropriate library for this.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript - ceiling of a dollar amount
I have a number like this: 360.654444444447
I want this to round up to 360.66
How do i do it? The amount of 4's between the 5 and 7 is unknown.
EDIT: The key issue here is that when the decimals after the 2nd would round to 5, it should round up. (ie: 360.654447 can be rounded to 360.655 - and that should round to 360.66
It's similar to the PHP_HALF_ROUND_MODE thing.
Use the ceil method, not the round method as others have suggested.
var number = 360.654444444447;
var result = Math.ceil(number * 100) / 100;