How do I determine number of integer digits and the number of digits after decimal in a number like 234.12413
in Java.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 8 years ago.
回答1:
A double is not always an exact representation. You can only say how many decimal places you would have if you converted it to a String.
double d= 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
This will only work for numbers which are not turned into exponent notation. You might consider 1.0 to have one or no decimal places.
回答2:
Double d = 234.12413;
String[] splitter = d.toString().split("\\.");
splitter[0].length(); // Before Decimal Count
splitter[1].length(); // After Decimal Count
回答3:
String s = "" + 234.12413;
String[] result = s.split("\\.");
System.out.println(result[0].length() + " " + result[1].length());
回答4:
1) convert to string
2) substring from '.' to end
3) get the length of this substring