このフォーマットを特定の整数に適用しようとしています#´###,###
私はDecimalFormat class
を試しましたが、数百万の場合はaccute accent
、千の場合はカンマの2つが必要な場合に1つのグループ分け記号しか使用できません。javaでint型の数値を書式設定するにはどうすればよいですか?
だから、最後に、私はたぶん、あなたがスペースまたはカンマの前にしたい単位で所定の位置に、この、「#」を使用してみてください、このよう1´000,000
このフォーマットを特定の整数に適用しようとしています#´###,###
私はDecimalFormat class
を試しましたが、数百万の場合はaccute accent
、千の場合はカンマの2つが必要な場合に1つのグループ分け記号しか使用できません。javaでint型の数値を書式設定するにはどうすればよいですか?
だから、最後に、私はたぶん、あなたがスペースまたはカンマの前にしたい単位で所定の位置に、この、「#」を使用してみてください、このよう1´000,000
私は、このソリューションは正規表現表現と
public static String audienceFormat(int number) {
String value = String.valueOf(number);
if (value.length() > 6) {
value = value.replaceFirst("(\\d{1,3})(\\d{3})(\\d{3})", "$1\u00B4$2,$3");
} else if (value.length() >=5 && value.length() <= 6) {
value = value.replaceFirst("(\\d{2,3})(\\d{3})", "$1,$2");
} else {
value = value.replaceFirst("(\\d{1})(\\d+)", "$1,$2");
}
return value;
}
このソリューションは、パフォーマンスへの影響を持っている場合、私はまた、私は正規表現でrockie午前、知らないreplaceFirstというメソッドを使用している、リンクはコメントで提供@Roshan有用であることが判明しましたこのコードは短絡されている可能性があります。
に1,000
または数百万のような値をフォーマットすることができます。
String num = "1000500000.574";
String newnew = new DecimalFormat("#,###.##").format(Double.parseDouble(number));
これはちょうど私に知らせてくれた場合 –
OPは特にDecimalFormatは動作しないと言った。 – walen
ありがとう@TiagoFutreしかし、私の特定のケースでは動作しないフォーマット –
私は常にString.formatを使用することをお勧めしますが、そのようなフォーマットのロケールがあるかどうかはわかりません。しかし、ここで仕事をするいくつかのコードがあります。
// Not sure if you wanted to start with a number or a string. Adjust accordingly
String stringValue = "1000000";
float floatValue = Float.valueOf(stringValue);
// Format the string to a known format
String formattedValue = String.format(Locale.US, "%,.2f", floatValue);
// Split the string on the separator
String[] parts = formattedValue.split(",");
// Put the parts back together with the special separators
String specialFormattedString = "";
int partsRemaining = parts.length;
for(int i=0;i<parts.length;i++)
{
specialFormattedString += parts[i];
partsRemaining--;
if(partsRemaining > 1)
specialFormattedString += "`";
else if(partsRemaining == 1)
specialFormattedString += ",";
}
これを試すと、Locale
のフォーマットが必要な形式になります。
List<Locale> locales = Arrays.asList(new Locale("it", "CH"), new Locale("fr", "CH"), new Locale("de", "CH"));
for (Locale locale : locales) {
DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
dfs.setCurrencySymbol("");
df.setDecimalFormatSymbols(dfs);
System.out.println(String.format("%5s %15s %15s", locale, format(df.format(1000)), format(df.format(1_000_000))));
}
utilの方法
private static String format(String str) {
int index = str.lastIndexOf('\'');
if (index > 0) {
return new StringBuilder(str).replace(index, index + 1, ",").toString();
}
return str;
}
出力
it_CH 1,000.00 1'000,000.00
fr_CH 1,000.00 1'000,000.00
de_CH 1,000.00 1'000,000.00
フラクション
出力
it_CH 1,000 1'000,000
fr_CH 1,000 1'000,000
de_CH 1,000 1'000,000
を除去する
df.setMaximumFractionDigits(0);
を設定しました
check @stackoverflow.com/questions/5114762/how-do-format-a-phone-number-as-a-string-in-java – Roshan
@Roshanその問題の解決法は、部分文字列() 'や' replace() 'は、電話番号" OK "でもいいかもしれませんが、実際の番号(例えば、お金)を扱うときはそれがベストアイデアではありません。 OPは、負の数値、小数点のない数値などを考慮する必要があります。 – walen
ありがとう@ロシャン! –