java 中价格格式化的 DecimalFormat

发布于 2024-08-27 03:00:21 字数 94 浏览 11 评论 0原文

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

Is it possible to format price according to rules like this using DecimalFormat in Java:
50000 => 50 000 rub 00 kop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-09-03 03:00:21
NumberFormat.getCurrencyInstance() 

可能会做你想做的事。

它可能会使用符号而不是文字来表示卢布和戈比。

这是一个工作示例:

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

对我来说这个输出:

50,000.00 卢布

这并不完全符合您的要求。但这是一个开始。

这个替代方案

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

给了我

50 000,01 руб

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 руб

叹梦 2024-09-03 03:00:21

假设您使用的是 .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));
%>

哪个输出;

 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

但不知道如何去掉小数位并忽略如果 kop == 0。

您还可以以不同的方式格式化 +/ve -/ve 字符串; http://blog.stevex.net/string-formatting-in-csharp/< /a>

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文