Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  156] [ 7]  / answers: 1 / hits: 63715  / 6 Years ago, mon, april 9, 2018, 12:00:00

My formatter looks like this





const formatter = new Intl.NumberFormat('en-NZ', {
style: 'currency',
currency: 'NZD',
minimumFractionDigits: 2,
});





How can I adjust this so that whole numbers have 0 fraction digits



formatter.format(4); // want output $4


whilst fractions always show two digits?



formatter.format(4.1); // want output $4.10

More From » javascript

 Answers
51

You could try something like this:



formatter.format(amount).replace(/D00$/, '');


Update:



In response to the many-months-later comment by @Marek, the above regex already handles differing decimal symbols (either . or ,), but it's true that it doesn't handle trailing currency symbols. That can be fixed this way:



formatter.format(amount).replace(/D00(?=D*$)/, '');

[#54720] Friday, April 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;