Saturday, December 9, 2017

JAVA - How to format currency

double value = 35.4567;
    
/* get current currency formatter */
    
NumberFormat currency = NumberFormat.getCurrencyInstance();        

/* format to currency */
    
System.out.println( currency.format( value ) );
Output:
35,46 Kč
With java.text.NumberFormat you can change currency or for example set minimal number of decimal places.
double value = 35.4567;
    
/* get current currency formatter */
    
NumberFormat currency = NumberFormat.getCurrencyInstance();        
    
/* set to UK currency */
    
currency.setCurrency( Currency.getInstance( Locale.UK ) );
currency.setMinimumFractionDigits( 3 );
    
/* format to currency */
    
System.out.println( currency.format( value ) );
Output:
35,457 GBP

No comments:

Post a Comment