[英]DecimalFormat - keep all decimal numbers
我正在使用DecimalFormat將雙精度格式化為String。 然后將此String集成到我的表示層中。
格式: #。## - >這使用小數點前的所有數字,但是將小數點后的數字換成ROUNDS 。
我可以使用#。#########作為大小數,但是如果小數更長的話怎么辦?
我發現我的小測試程序很有用,並希望與您分享。
你能幫我顯示所有小數嗎?
package be.softwarelab.numbers;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class DecimalNumbersTest {
private static final double NUMBER = 123456789.123456789;
public static void main(String[] args) {
String format01 = "#.";
String format02 = "#.#";
String format03 = "#.##";
String format04 = "#.###";
String format05 = "#.####";
System.out.println("====== NUMBER ===== USA =====================================");
showResult(NUMBER, format01, Locale.US);
showResult(NUMBER, format02, Locale.US);
showResult(NUMBER, format03, Locale.US);
showResult(NUMBER, format04, Locale.US);
showResult(NUMBER, format05, Locale.US);
System.out.println("====== NUMBER ===== France ==================================");
showResult(NUMBER, format01, Locale.FRANCE);
showResult(NUMBER, format02, Locale.FRANCE);
showResult(NUMBER, format03, Locale.FRANCE);
showResult(NUMBER, format04, Locale.FRANCE);
showResult(NUMBER, format05, Locale.FRANCE);
System.out.println("=============================================================");
}
public static void showResult(double number, String format, Locale locale) {
// Using a Locale to see the differences between regions.
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(locale);
DecimalFormat formatter = new DecimalFormat (format, otherSymbols);
// Create the String result
String output = formatter.format(number);
// Format the output for a nice presentation.
System.out.format(" %s %11s = %20s\n", locale, format, output);
}
}
這導致:
====== NUMBER ===== USA =====================================
en_US #. = 123456789.
en_US #.# = 123456789.1
en_US #.## = 123456789.12
en_US #.### = 123456789.123
en_US #.#### = 123456789.1235
====== NUMBER ===== France ==================================
fr_FR #. = 123456789,
fr_FR #.# = 123456789,1
fr_FR #.## = 123456789,12
fr_FR #.### = 123456789,123
fr_FR #.#### = 123456789,1235
=============================================================
編輯:一位用戶提到了一個相關的問題: 如何很好地將浮動數字格式化為字符串而沒有不必要的小數0? 這個問題並沒有解決我的問題,因為它側重於限制尺寸,但我需要盡可能長時間地保持它。
String.format可能會幫到你 - NumberFormat
private static void printfWithLocale(Locale locale, Double d){
System.out.println("Output locale: " + locale.toString());
String simpleOutputTeplate = "simpleOutputTeplate: %s";
String refinedOutputTeplate = "refinedOutputTeplate: %.10f";
System.out.println(String.format(locale, simpleOutputTeplate, d));
System.out.println(String.format(locale, refinedOutputTeplate, d));
}
public static void main(String[] args) {
Double d = new Double(3.1234567890123456789);
printfWithLocale(Locale.US, d);
System.out.println("");
printfWithLocale(Locale.FRANCE, d);
}
代碼輸出:
Output locale: en_US
simpleOutputTeplate: 3.1234567890123457
refinedOutputTeplate: 3.1234567890
Output locale: fr_FR
simpleOutputTeplate: 3.1234567890123457
refinedOutputTeplate: 3,1234567890
您會注意到%s(字符串)不符合Locale,但會將Double格式化為最多17個小數點。 使用String.format,您可以進一步細化數字在字符串中的格式。
由於double並不完全代表某些數字,因此如果切割到給定的小數長度(如17)則無關緊要。 請參閱這個解釋的另一個問題有多少有效數字在java中有浮點數和雙精度數?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.