How to Format Numbers as Currency Strings

How to format numbers as currency strings

Ok, based on what you said, I'm using this:

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);

var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return '£ ' + intPart + DecimalSeparator + decPart;

I'm open to improvement suggestions (I'd prefer not to include YUI just to do this :-) )

I already know I should be detecting the "." instead of just using it as the decimal separator...

How to format numbers as currency strings

Ok, based on what you said, I'm using this:

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);

var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return '£ ' + intPart + DecimalSeparator + decPart;

I'm open to improvement suggestions (I'd prefer not to include YUI just to do this :-) )

I already know I should be detecting the "." instead of just using it as the decimal separator...

How to format decimals in a currency format?

I doubt it. The problem is that 100 is never 100 if it's a float, it's normally 99.9999999999 or 100.0000001 or something like that.

If you do want to format it that way, you have to define an epsilon, that is, a maximum distance from an integer number, and use integer formatting if the difference is smaller, and a float otherwise.

Something like this would do the trick:

public String formatDecimal(float number) {
float epsilon = 0.004f; // 4 tenths of a cent
if (Math.abs(Math.round(number) - number) < epsilon) {
return String.format("%10.0f", number); // sdb
} else {
return String.format("%10.2f", number); // dj_segfault
}
}

How do I print currency format in JavaScript

You've got to do this by hand, there is nothing builtin into JS. For an example look at this post here: How can I format numbers as money in JavaScript?

Javascript How To Format An Integer As A Currency String?

You can use toLocaleString() function for this.

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

Format string number as currency

Here is a two-step solution: get the digits sorted into 3+ digit numbers and 1-2 digit numbers with

^(?:(\d+)(\d{2})|(\d{1,2}))$

See the regex demo

So, the numbers bigger than 1 will have their integer parts in Group 1 and decimal parts in Group 2, and Group 3 will hold all fractional numbers less than 0.

Then, we check the number we get and only add thousand separators to the integer parts with

.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.")

See the JS demo:

function format(s) {

var re = /^(?:(\d+)(\d{2})|(\d{1,2}))$/;

if ((m = s.match(re)) !== null) {

if (m[3]) { // We have 1 or 2 digit number

return m[3].length == 1 ?

"0,0" + m[3] : "0," + m[3];

} else { // We have a longer number

return m[1].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.") + "," + m[2];

}



}

}

document.body.innerHTML = format('1') + "<br/>"; // '0,01'

document.body.innerHTML += format('12') + "<br/>"; // '0,12'

document.body.innerHTML += format('123') + "<br/>"; // '1,23'

document.body.innerHTML += format('12345678') + "<br/>"; // '123.456,78'

How to format string to money

Convert the string to a decimal then divide it by 100 and apply the currency format string:

string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);

Edited to remove currency symbol as requested and convert to decimal instead.

How to format numbers as currency strings in Flutter

Maybe the NumberFormat class will help you: https://api.flutter.dev/flutter/intl/NumberFormat-class.html

Did you try the examples?



Related Topics



Leave a reply



Submit