Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a whole number amount of cents to a readable dollar amount in JavaScript?

Tags:

var num = 1629; // this represents $16.29 num.toLocaleString("en-US", {style:"currency", currency:"USD"}); // outputs $1,629 

So far this is as close as I can come. I tried all of the options that toLocaleString provides but there seems to be no easy way to get the outcome I want (which is not as expected). Is there no built-in function that exists in JS?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

like image 637
whocodes Avatar asked Sep 24 '15 18:09

whocodes


People also ask

How do you write USD amounts with cents?

Use Numbers for CentsOne thousand two hundred thirty-four dollars and 56/100. One thousand two hundred thirty-four dollars + 56/100.

How do you convert cents to dollars and cents in Java?

Use division by 100 in combination with the 'int' primitive type to see how many dollars you have. For example, int dollars = cents/100; No matter what (int) value you have for cents, at the end of that operation, dollars will be an integer, so it will have the exact amount of dollars.

How do you convert cent?

In general, one cent equals 435.6 square feet. 0.0022959 cents is the value of one square feet. To convert cents to square feet, multiply the area in cents by 435.6. Multiply the area in square feet by 0.0023 to convert square feet to cents.

How do you convert to cents in Java?

What is the best way to convert Dollar which is a double value to cents which is an int value in Java. Currently I use the following approach: Double cents = new Double(dollar*100); int amount = cents. intValue();


2 Answers

Try dividing the number of cents by 100 to get the dollar equivalent. I.E.:

var num = 1629; var dollars = num / 100; dollars = dollars.toLocaleString("en-US", {style:"currency", currency:"USD"}); 

dollars now equals "$16.29"

like image 124
amklose Avatar answered Sep 22 '22 18:09

amklose


Why not divide through 100 before toLocaleString?

var num = 1629; // this represents $16.29 num /= 100; // cent to dollar num.toLocaleString("en-US", {style:"currency", currency:"USD"}); 
like image 21
ManfredP Avatar answered Sep 22 '22 18:09

ManfredP