Java String.format()

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

Java String.format()

问题

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

String amount = "355128000";

我想它格式化成这样:

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

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

String amount = "355128000";

I want it to be formatted like this way:

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:

String amount = "355128000";
double amountDouble = Double.parseDouble(amount);

String formattedAmount = String.format("%,.2f", amountDouble);
// 355,128,000.00

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

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':

String amount = "355128000";
double amountDouble = Double.parseDouble(amount);

String formattedAmount = String.format("%,.2f", amountDouble);
// 355,128,000.00

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


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:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample {

    private static final DecimalFormat numberFormatter;

    static {
        numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);

        // Configure/override
        DecimalFormatSymbols symbols = numberFormatter.getDecimalFormatSymbols();
        symbols.setGroupingSeparator('\u00a0'); // non-breaking space
        symbols.setDecimalSeparator(',');
        numberFormatter.setDecimalFormatSymbols(symbols);
        numberFormatter.setMinimumFractionDigits(2);
    }

    public static String formatNumber(String rawNumber) {
        return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
    }

    public static void main(String[] args) {
        String amount = "355128000";
        System.out.println(formatNumber(amount)); // 3 551 280,00
    }
}

Edit

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

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample {

    private static final DecimalFormat numberFormatter;

    static {
        numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);

        // Configure
        numberFormatter.setMinimumFractionDigits(2);
    }

    public static String formatNumber(String rawNumber) {
        return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
    }

    public static void main(String[] args) {
        String amount = "355128000";
        System.out.println(formatNumber(amount)); // 3 551 280,00
    }
}
英文:

You can create an instance of NumberFormatter and specify options:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample {

    private static final DecimalFormat numberFormatter;

    static {
        numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);

        // Configure/override
        DecimalFormatSymbols symbols = numberFormatter.getDecimalFormatSymbols();
        symbols.setGroupingSeparator('\u00a0'); // non-breaking space
        symbols.setDecimalSeparator(',');
        numberFormatter.setDecimalFormatSymbols(symbols);
        numberFormatter.setMinimumFractionDigits(2);
    }

    public static String formatNumber(String rawNumber) {
        return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
    }

    public static void main(String[] args) {
        String amount = "355128000";
        System.out.println(formatNumber(amount)); // 3 551 280,00
    }
}

Edit

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

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample {

    private static final DecimalFormat numberFormatter;

    static {
        numberFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);

        // Configure
        numberFormatter.setMinimumFractionDigits(2);
    }

    public static String formatNumber(String rawNumber) {
        return numberFormatter.format(Long.parseLong(rawNumber) / 100d);
    }

    public static void main(String[] args) {
        String amount = "355128000";
        System.out.println(formatNumber(amount)); // 3 551 280,00
    }
}

答案3

得分: 0

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

String amount = "355128000";

UnaryOperator<String> format = str -> {
    long v = Long.parseLong(str);
    return String.format(Locale.FRANCE, "%,.2f", v / 100.);
};

System.out.println(format.apply(amount));

输出结果为

3 551 280,00
英文:

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

String amount = &quot;355128000&quot;;

UnaryOperator&lt;String&gt; format = str -&gt; {
    long v = Long.parseLong(str);
    return String.format(Locale.FRANCE, &quot;%,.2f&quot;, v / 100.);
};


System.out.println(format.apply(amount));

prints

3 551 280,00

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

发表评论

匿名网友

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

确定