DecimalFormat for formatting prices in java

Is it possible to format the price in accordance with such rules as DecimalFormat in Java: 50,000 => 50,000 rubles. 00 kopecks

+3
source share
2 answers
NumberFormat.getCurrencyInstance() 

probably doing what you want.

It can use characters instead of words for rubles and pennies.

Here is a working example:

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

This conclusion is for me:

RUB50,000.00

which is not exactly what you asked for. But this is the beginning.

This alternative

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

gave me

50 000,01 rub

+4
source

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

<%
  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));
%>

What are the exits;

 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 point, although omit it if kop == zero.

+/ve -/ve -; http://blog.stevex.net/string-formatting-in-csharp/

0

Source: https://habr.com/ru/post/1738940/


All Articles