简体   繁体   中英

Format String currency

I am trying to display 3000 as €300,0 using the code below :

String formattedString = String.format("%.2€;",3000);  
System.out.println("format: "+formattedString);

but result in force close error.

what am I missing?

I would recommend using a locale-specific currency format instead

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.getDefault());

Better I18N that way.

Check out the Java API: NumberFormat . It will show how to use predefined formats top achieve your goal.

Try something like this:

import java.text.NumberFormat;
import java.util.Locale;

Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);//use Locale.getDefault() if you didn't know the type
System.out.println(fmt.format(100.00));

This will print: £100.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM