假设我有 3.13 和 4.13,我希望能够检查 .13 是否来自 3.13 和 .13 是否来自 4.13。
我尝试了很多东西:
1) 将两位小数转换为字符串并尝试用“.”将它们分开,但我无法让它工作
2) a = 3.14;
a = a - Math.floor(a);
尝试单独获取小数但我最终得到 0.1400000001
最佳答案
converting the two decimals to Strings and trying to split them up by ".", but I couldnt get that to work
split
使用正则表达式,因此您必须转义点。
string.split(".")
应该变成
string.split("\\.")
有了这个你应该能够正确地分割字符串并进行比较
顺便说一下,我会使用 Reimenus solution ,当你有数字时,如果可以的话,最好使用数学。 仅在您确实需要时才使用字符串。
关于java - 比较前两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826690/