我想使用DecimalFormat
,它以工程表示法(指數是3的倍數)格式化數字,並使用固定數量的小數位數。小數位數應該可以由模式配置。使用固定小數位數的工程符號格式的雙精度格式
這是可能與java.text.DecimalFormat
類。 有其他選擇嗎?
這裏是輸出12.345E3代替12.34E3測試用例:
public static void main(String[] args)
{
Locale.setDefault(Locale.ENGLISH);
DecimalFormat df = new DecimalFormat("##0.00E0");
String realOutput = df.format(12345);
String expected = "12.34E3";
System.out.println(realOutput);
if (Objects.equals(realOutput, expected))
{
System.out.println("OK");
}
else
{
System.err.println("Formatted output " + realOutput + " differs from documented output " + expected);
}
}
[谷歌是你的朋友(http://www.labbookpages.co.uk/software/java/engNotation.html) – specializt
谷歌僅僅是你的第二個最佳朋友。你最好的朋友是具有幾乎所有「內置」類/概念的傑出javadoc。 – GhostCat
Javadoc沒有幫我找到解決方案。嘗試使用工程符號和兩個小數數字來尋找一個模式,這兩個數字適用於兩個數字:12345和1234.工程符號不起作用或小數位數不能修復。 – Uli