Java Double 获取点/逗号后的所有数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15451492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Double get all Digits after dot/comma
提问by Alex Tape
it′s a simple task but i′m not able to solve it on my own..
这是一个简单的任务,但我无法自己解决它..
i got
我有
double digit1 = 12.1;
double digit2 = 12.99;
and need a method which gives me this:
并且需要一种方法来给我这个:
anyMethod(digit1); //returns 10
anyMethod(digit2); //returns 99
what i have is
我所拥有的是
public static void getAfterComma(double digit) {
BigDecimal bd = new BigDecimal(( digit - Math.floor( digit )) * 100 );
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println(bd.toBigInteger()); // prints digit1=1 and digit2=99
}
anyway i prefer integer as the returntype.. anybody got a quick solution/tip?
无论如何,我更喜欢整数作为返回类型..有人有快速解决方案/提示吗?
kindly
亲切地
回答by Sazzadur Rahaman
Why not you simply use:
为什么不简单地使用:
int anyMethod(double a){
//if the number has two digits after the decimal point.
return (int)((a + 0.001) * 100) % 100;
}
回答by Mark Slater
A simple way of getting the fractional part of a double
is to use the modulo operator, %
. However, you'll have to take into account the fact that floating point arithmetic isn't precise. For example,
获取 a 的小数部分的一种简单方法double
是使用模运算符%
。但是,您必须考虑到浮点运算并不精确这一事实。例如,
System.out.println(12.1 % 1); // outputs 0.09999999999999964
System.out.println(12.99 % 1); // outputs 0.9900000000000002
If you want to get two decimal digits as an int
, which is what I think you're asking, you can achieve this, glossing over the floating point issues, like so:
如果您想将两位十进制数字作为int
,这就是我认为您要问的,您可以实现这一点,掩盖浮点问题,如下所示:
System.out.println(Math.round((12.1 % 1) * 100)); // outputs 10
System.out.println(Math.round((12.99 % 1) * 100)); // outputs 99
However, you should consider going further down the BigDecimal
path you started down, which uses arbitrary precision arithmetic. You could do something like this:
但是,您应该考虑在BigDecimal
您开始的道路上走得更远,它使用任意精度算法。你可以这样做:
System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE)); // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE)); // outputs 0.99
If, as before, you want two decimal digits from this, you can do this:
如果像以前一样,您想要两个十进制数字,您可以这样做:
System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue()); // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue()); // outputs 0.99
Note that there a couple of differences between these last two methods and the first two: they preserve the sign of the argument, so if you use the final example for -12.99, you'll get -99 back, and they treat the fractional part of an integer as 1, so if you use the final example for 12, you'll get 100 back.
请注意,最后两种方法与前两种方法之间存在一些差异:它们保留了参数的符号,因此如果您使用 -12.99 的最后一个示例,您将得到 -99,并且它们会处理小数部分一个整数为 1,所以如果你使用最后一个例子 12,你会得到 100。
回答by Kirin Yao
It's not necessary to use Number
tyeps all the time. You can take advantage of String
as a mediator.
没有必要一直使用Number
类型。您可以利用String
作为调解人。
String mediator = Double.valueOf(d1).toString();
mediator = mediator.substring(mediator.indexOf('.') + 1);
Integer result = Integer.valueOf(mediator);
回答by Sach
Try this out
试试这个
public static void main(String args[]){
double a=12.99;
double b=12.1;
System.out.println(method(a));
System.out.println(method(b));
}
private static int method(double a) {
return (int) ((a*100)%100);
}
回答by Barranka
If I understand correctly, you need to return n
digits after the dot for a given double
number. So... let's see:
如果我理解正确,您需要n
在给定数字的点后返回数字double
。所以……让我们看看:
public int decimalDigits(double x, int n) {
double ans;
ans = (x - (int) x) * Math.pow(10, n);
return (int) ans;
}
Done. Hope this helps you.
完毕。希望这对你有帮助。
For your specific example, 'n = 2' should do.
对于您的具体示例, 'n = 2' 应该可以。
回答by Alex Tape
sachin-pasalkar done it! little fix but fine!
sachin-pasalkar 做到了!小修复,但很好!
public static int anyMethod(double a){
return (int) (a*100)%100;
}
回答by VishalDevgire
check out this code returns digits after '.' always. Without any extra parameters other than double variable.
查看此代码返回“。”后的数字 总是。除了双变量之外没有任何额外的参数。
public int anyMethod(double d)
{
String numString = d+"";
return Integer.parseInt(numString.substring(numString.indexOf('.')+1));
}