英文:
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 = "355128000";
UnaryOperator<String> format = str -> {
long v = Long.parseLong(str);
return String.format(Locale.FRANCE, "%,.2f", v / 100.);
};
System.out.println(format.apply(amount));
prints
3 551 280,00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论