Java String.format()

huangapple go评论94阅读模式
英文:

Java String.format()

问题

我有一个字符串,我想以特定的方式格式化它。
这是我的字符串:

  1. String amount = "355128000";

我想它格式化成这样:

  1. String formattedAmount = "3 551 280,00";
英文:

I have a string and I want to format it in a custom way.
This is my String:

  1. String amount = "355128000";

I want it to be formatted like this way:

  1. String formattedAMount = "3 551 280,00"

With a space after first number and a group of 3 numbers and a comma at the end before two nubmers

答案1

得分: 0

You can format it with String.format() if you first convert it to double:

  1. String amount = "355128000";
  2. double amountDouble = Double.parseDouble(amount);
  3. String formattedAmount = String.format("%,.2f", amountDouble);
  4. // 355,128,000.00

if you want to look it exactly like as you showed you can achieve this by:

  1. String formattedAmount = String.format("%,.2f", amountDouble).replace(",", " ").replace(".", ",");

But String.format() might not be the best way for this purpose, use DecimalFormat instead.

英文:

You can format it with String.format() if you first convert it to `double':

  1. String amount = "355128000";
  2. double amountDouble = Double.parseDouble(amount);
  3. String formattedAmount = String.format("%,.2f", amountDouble);
  4. // 355,128,000.00

if you want to look it exactly like as you showed you can achieve this by:

  1. String formattedAmount = String.format("%,.2f", amountDouble).replace(",", " ").replace(".", ",");

But String.format() might not be the best way for this purpose, use DecimalFormat instead.

答案2

得分: 0

You can create an instance of NumberFormatter and specify options:

  1. import java.text.DecimalFormat;
  2. import java.text.DecimalFormatSymbols;
  3. import java.text.NumberFormat;
  4. import java.util.Locale;
  5. public class NumberFormatExample {
  6. private static final DecimalFormat numberFormatter;
  7. static {
  8. numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
  9. // Configure/override
  10. DecimalFormatSymbols symbols = numberFormatter.getDecimalFormatSymbols();
  11. symbols.setGroupingSeparator('\u00a0'); // non-breaking space
  12. symbols.setDecimalSeparator(',');
  13. numberFormatter.setDecimalFormatSymbols(symbols);
  14. numberFormatter.setMinimumFractionDigits(2);
  15. }
  16. public static String formatNumber(String rawNumber) {
  17. return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
  18. }
  19. public static void main(String[] args) {
  20. String amount = "355128000";
  21. System.out.println(formatNumber(amount)); // 3 551 280,00
  22. }
  23. }

Edit

It looks like you can use the French formatter, and specify just the min fraction digits.

  1. import java.text.DecimalFormat;
  2. import java.text.NumberFormat;
  3. import java.util.Locale;
  4. public class NumberFormatExample {
  5. private static final DecimalFormat numberFormatter;
  6. static {
  7. numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);
  8. // Configure
  9. numberFormatter.setMinimumFractionDigits(2);
  10. }
  11. public static String formatNumber(String rawNumber) {
  12. return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
  13. }
  14. public static void main(String[] args) {
  15. String amount = "355128000";
  16. System.out.println(formatNumber(amount)); // 3 551 280,00
  17. }
  18. }
英文:

You can create an instance of NumberFormatter and specify options:

  1. import java.text.DecimalFormat;
  2. import java.text.DecimalFormatSymbols;
  3. import java.text.NumberFormat;
  4. import java.util.Locale;
  5. public class NumberFormatExample {
  6. private static final DecimalFormat numberFormatter;
  7. static {
  8. numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
  9. // Configure/override
  10. DecimalFormatSymbols symbols = numberFormatter.getDecimalFormatSymbols();
  11. symbols.setGroupingSeparator('\u00a0'); // non-breaking space
  12. symbols.setDecimalSeparator(',');
  13. numberFormatter.setDecimalFormatSymbols(symbols);
  14. numberFormatter.setMinimumFractionDigits(2);
  15. }
  16. public static String formatNumber(String rawNumber) {
  17. return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
  18. }
  19. public static void main(String[] args) {
  20. String amount = "355128000";
  21. System.out.println(formatNumber(amount)); // 3 551 280,00
  22. }
  23. }

Edit

It looks like you can use the French formatter, and specify just the min fraction digits.

  1. import java.text.DecimalFormat;
  2. import java.text.NumberFormat;
  3. import java.util.Locale;
  4. public class NumberFormatExample {
  5. private static final DecimalFormat numberFormatter;
  6. static {
  7. numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);
  8. // Configure
  9. numberFormatter.setMinimumFractionDigits(2);
  10. }
  11. public static String formatNumber(String rawNumber) {
  12. return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
  13. }
  14. public static void main(String[] args) {
  15. String amount = "355128000";
  16. System.out.println(formatNumber(amount)); // 3 551 280,00
  17. }
  18. }

答案3

得分: 0

我会放弃使用 NumberFormat,只需按照以下方式使用与您期望的格式相匹配的 France locale 进行操作。

  1. String amount = "355128000";
  2. UnaryOperator<String> format = str -> {
  3. long v = Long.parseLong(str);
  4. return String.format(Locale.FRANCE, "%,.2f", v / 100.);
  5. };
  6. System.out.println(format.apply(amount));

输出结果为

  1. 3551280,00
英文:

I would forgo NumberFormat and just do it as follows using the France locale which matches your desired format.

  1. String amount = &quot;355128000&quot;;
  2. UnaryOperator&lt;String&gt; format = str -&gt; {
  3. long v = Long.parseLong(str);
  4. return String.format(Locale.FRANCE, &quot;%,.2f&quot;, v / 100.);
  5. };
  6. System.out.println(format.apply(amount));

prints

  1. 3551280,00

huangapple
  • 本文由 发表于 2023年4月17日 21:12:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定