简体   繁体   English

DecimalFormat用于java中的价格格式

[英]DecimalFormat for price formatting in java

是否可以使用Java中的DecimalFormat根据这样的规则格式化价格: 50000 => 50 000 rub 00 kop

NumberFormat.getCurrencyInstance() 

probably does what you want. 可能做你想要的。

It might use symbols instead of words for roubles and kopeks. 对于卢布和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 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 руб 50000,01руб

Assuming you are using .Net you can format the output using; 假设您正在使用.Net,您可以使用;

<%
  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. 不知道怎么摆脱小数位但是如果kop ==零则省略。

You can also format +/ve -/ve strings differently see; 你也可以用不同的方式格式化+ / ve - / ve字符串; http://blog.stevex.net/string-formatting-in-csharp/ http://blog.stevex.net/string-formatting-in-csharp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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