How to use Java's DecimalFormat for "smart" currency formatting? -


i'd use java's decimalformat format doubles so:

#1 - 100 -> $100 #2 - 100.5 -> $100.50 #3 - 100.41 -> $100.41 

the best can come far is:

new decimalformat("'$'0.##"); 

but doesn't work case #2, , instead outputs "$100.5"

edit:

a lot of these answers considering cases #2 , #3 , not realizing solution cause #1 format 100 "$100.00" instead of "$100".

does have use decimalformat?

if not, looks following should work:

string currencystring = numberformat.getcurrencyinstance().format(currencynumber); //handle weird exception of formatting whole dollar amounts no decimal currencystring = currencystring.replaceall("\\.00", ""); 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -