java - 比较前两位小数

标签 java split decimal

假设我有 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/

相关文章:

java - 可以将 HTML 样式的链接添加到 SWT StyledText 中吗?

string - As3在回车票上拆分

java - 使用 java String.split 从文件动态读取

c - C中删除所有小数

c# - 如何在 Pi 的 C# 程序中将小数点精确到 n 位

java - 从 Java 执行 .app 文件夹?

java - Akka:我应该在 Actor 中使用parallelStream还是executors

MySQL 将浮点值内部四舍五入到小数点后一位

java - ClassNotFoundException HadoopMapReduceCommitProtocol

python 2 : AttributeError: 'file' object has no attribute 'strip'