双数位数字数 [英] Number of decimal digits in a double
本文介绍了双数位数字数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
我需要数字的整数数字和十进制数字后的数字,如234.12413。双重并不总是精确的表示。你只能说你把它转换成一个字符串,你会有几个小数位。
double d = 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('。');
int decimalPlaces = text.length() - integerPlaces - 1;
这只适用于没有变成指数符号的数字。您可以考虑使用1.0或小数位数。
I need the number of integer digits and the number of digits after decimal in a number like 234.12413 in Java.
解决方案
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.
这篇关于双数位数字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文