m n*_*m n -1 java string double
我已经在互联网上搜索过,但找不到任何解决方案(也许,我搜索得很糟糕)。
我想将 转换String "108595000.5"为 adouble并且我使用了这些方法:
Double.parseDouble("108595000.5");
Double.valueOf("108595000.5");
Run Code Online (Sandbox Code Playgroud)
可惜,两人都回来了1.08595E8。
我怎样才能毫无问题地将其转换String为?double
您使用的方法不返回1.08595E8,而是返回数字,您抱怨的是该数字在控制台中的表示(或作为String)。
但是,您可以指定如何double自己以指定的格式输出,请参阅此示例:
public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
Run Code Online (Sandbox Code Playgroud)
public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
Run Code Online (Sandbox Code Playgroud)