开发者

DecimalFormat for price formatting in java

开发者 https://www.devze.com 2022-12-24 06:23 出处:网络
Is it pos开发者_如何学Gosible to format price according to rules like this using DecimalFormat in Java:

Is it pos开发者_如何学Gosible to format price according to rules like this using DecimalFormat in Java: 50000 => 50 000 rub 00 kop


NumberFormat.getCurrencyInstance() 

probably does what you want.

It might use symbols instead of words for roubles and kopeks.

Here's a working example:

    final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance();
    currencyInstance.setCurrency(Currency.getInstance("RUB"));
    System.out.println(currencyInstance.format(50000));

This output for me:

RUB50,000.00

which is not exactly what you asked for. But it is a start.

This alternative

final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(new Locale("ru", "RU"));
System.out.println(currencyInstance.format(50000.01));

gave me

50 000,01 руб


Assuming you are using .Net you can format the output using;

<%
  String format = "<p>{0:#,0 rub .00 kop}</p>";
  Response.Write(String.Format(format, 98765.4321));
  Response.Write(String.Format(format, 98765.4));
  Response.Write(String.Format(format, 987654321));
  Response.Write(String.Format(format, 0.12345));
  Response.Write(String.Format(format, 0.1));
  Response.Write(String.Format(format, 0));
%>

Which outputs;

 98,765 rub .43 kop
 98,765 rub .40 kop
 987,654,321 rub .00 kop
 0 rub .12 kop
 0 rub .10 kop
 0 rub .00 kop

Not sure how to get rid of the decimal place though and omit if kop == zero.

You can also format +/ve -/ve strings differently see; http://blog.stevex.net/string-formatting-in-csharp/

0

精彩评论

暂无评论...
验证码 换一张
取 消