在Java中是否有内置函数可以告诉我一个double类型变量有多少小数位。例如:
101.13 = 2
101.130 = 3
1.100 = 3
1.1 = 1
-3.2322 = 4 etc.
如果需要的话,我很乐意先转换为另一种类型,我已经尝试过先将其转换为BigDecimal,但没有成功。
如果你像这样使用字符串形式的数字,你可以使用BigDecimal.scale():
BigDecimal a = new BigDecimal("1.31");
System.out.println(a.scale()); //prints 2
BigDecimal b = new BigDecimal("1.310");
System.out.println(b.scale()); //prints 3
但如果您已经将数字作为字符串,最好使用正则表达式解析该字符串以查看有多少位数字:
String[] s = "1.31".split("\\.");
System.out.println(s[s.length - 1].length());
使用BigDecimal可能具有优点,它可以检查字符串是否实际上是一个数字;使用字符串方法,您必须自己进行检查。而且,如果您将数字作为双精度数,则无法区分1.31
和1.310
(它们是完全相同的双精度数),正如其他人也指出的那样。
不。
100和1.1在计算机中被表示为相同的位,因此它们代表了完全相同的值(在双精度浮点数 double
中)。
所以你永远无法从一个 double
中获取那种信息。
你唯一能做的是获取将一个十进制数解析为相同 double
值所需的最小十进制数字数。这可以通过调用 Double.toString()
并检查有多少十进制数字来轻松完成。
double类型的小数位数为16。
64位数字。52位尾数。52位大约是16个十进制数字。
参见http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html。
double类型的值包括64位IEEE 754浮点数。
不,我不知道有内置的函数可以实现这个功能。
不过,有一种简单的方法可以做到。Double.toString会给你一个包含双精度浮点数中所有有效十进制数字的字符串。以下是该字符串的一些属性:
使用Double.toString来确定有多少位小数实质上包括小数点右侧的有效数字减去科学计数法指数(如果有)。十进制表示法始终至少有一个数字在小数点右侧,并且至少有一个数字在小数点左侧,即使它是零。由于我们关心的是有效数字的小数位数,因此小数点右侧的尾随零是一个问题,不应被计算为小数位。
以下代码将为您进行良好的计算:
StringBuffer stringBuffer = new StringBuffer(Double.toString(1234.567890D));
System.out.println(stringBuffer.toString());
int i; // general purpose character index
int exponent;
int decimalPlaces;
if ((i = stringBuffer.indexOf("E")) > -1) { // scientific notation...
// turn scientific notation exponent into an integer
exponent = Integer.parseInt(stringBuffer.substring(i + 1));
// truncate the exponent from the StringBuffer
stringBuffer = stringBuffer.delete(i, stringBuffer.length());
} else { // decimal notation, could be trailing zero
exponent = 0; // no exponent, so zero
// point i to trailing zero and truncate it, if there is one
if (stringBuffer.charAt((i = stringBuffer.length() - 1)) == '0') {
stringBuffer = stringBuffer.deleteCharAt(i); // delete trailing zero
}
}
// stringBuffer now contains only significant digits to the
// right of the decimal point, if there are any
decimalPlaces = stringBuffer.length() - 1 - stringBuffer.indexOf(".") - exponent;
// zero or positive number is decimal places
// negative number is number of zeroes to the left of the decimal point
// between the decimal point and the least significant digit
System.out.println(decimalPlaces);
// ****************************************************************
public int getDecimals(double doubleValue) {
// ****************************************************************
BigDecimal bd1 = new BigDecimal(Double.toString(doubleValue)).stripTrailingZeros();
return bd1.scale();
}
public class test {
public static void main(String[] args) {
double x;`enter code here`
x = 3411.999999999999;
System.out.println("16: "+x); // gives 3411.999999999999
x = 3411.9999999999999;
System.out.println("17: "+x); // gives 3412.0
x = 0.9999999999999999;
System.out.println("16: "+x); // gives 0.9999999999999999
x = 0.99999999999999999;
System.out.println("17: "+x); // gives 1.0
}
}
这里有16位数字,计算结果为3411.999999999999。
现在在小数点后面再加一个9,总共17位数字,即3411.9999999999999,重新运行。打印出的值为3412.0。在这种情况下,我们超载了x的内部表示,并且数字被舍入以存储。
println忠实地打印它内部看到的内容。只有64位来保存双精度浮点数(尾数和指数——请参阅IEEE 754了解详细信息)。
玩弄x的值,您将看到效果。例如,0.9999999999999999(16个9)输出0.9999999999999999;0.99999999999999999(17个9)输出1.0。
希望这能帮到您。
StringBuffer stringBuffer = new StringBuffer(Double.toString(ratioGrossYield));
int i; // general purpose character index
int exponent;
int decimalPlaces;
if ((i = stringBuffer.indexOf("E")) > -1) { // scientific notation...
// turn scientific notation exponent into an integer
exponent = Integer.parseInt(stringBuffer.substring(i + 1));
// truncate the exponent from the StringBuffer
stringBuffer = stringBuffer.delete(i, stringBuffer.length());
} else { // decimal notation, could be trailing zero
exponent = 0; // no exponent, so zero
// point i to trailing zero and truncate it, if there is one
if (stringBuffer.charAt((i = stringBuffer.length() - 1)) == '0') {
stringBuffer = stringBuffer.deleteCharAt(i); // delete trailing zero
}
}
// stringBuffer now contains only significant digits to the
// right of the decimal point, if there are any
decimalPlaces = stringBuffer.length() - 1 - stringBuffer.indexOf(".") - exponent;
// zero or positive number is decimal places
// negative number is number of zeroes to the left of the decimal point
// between the decimal point and the least significant digit
if (stringBuffer.charAt(stringBuffer.length() - 1) == '0') {
return decimalPlaces-1;
} else {
return decimalPlaces;
}