在java中删除数值后的小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18865501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:45:26  来源:igfitidea点击:

Remove decimals after a numeric value in java

javaexceldecimal

提问by Bimbz

Hi I have a excel file reading application which reads every cell in the file.

嗨,我有一个 excel 文件读取应用程序,它读取文件中的每个单元格。

whenever a cell contains a numeric value the app is treating it a numeric cell.

每当一个单元格包含一个数值时,应用程序就会将其视为一个数字单元格。

For example the cell contains (40002547) the application will treat this as numeric cell. I cab get the value by using this code:

例如,单元格包含 (40002547),应用程序会将其视为数字单元格。我通过使用以下代码获取值:

SONum = String.valueOf(cellSONum.getNumericCellValue());

Well that works fine. My Problem is it appends decimal at the end of the string. it will be (40002547.0). I need it to be as is. Thanks in advance

嗯,效果很好。我的问题是它在字符串的末尾附加了十进制。它将是 (40002547.0)。我需要它保持原样。提前致谢

采纳答案by paxdiablo

It's because cellSONum.getNumericCellValue()is returning a floating point type. If you force it to an integer beforecalling valueOf(), you should get the string representation in an integral form, if indeed that's what you want for allpossibilities:

这是因为cellSONum.getNumericCellValue()正在返回一个浮点类型。如果调用之前将其强制为整数valueOf(),则应该以整数形式获取字符串表示形式,如果这确实是您想要的所有可能性:

SONum = String.valueOf((int)cellSONum.getNumericCellValue());

You can see this in the following code:

您可以在以下代码中看到这一点:

class Test {
    public static void main(String[]args) {
        double d = 1234;
        System.out.println(String.valueOf(d));
        System.out.println(String.valueOf((int)d));
    }
}

which outputs:

输出:

1234.0
1234


However, if you want to just get rid of .0at the end of any string but allow non-integral values to survive, you can just remove the trailing text yourself:

但是,如果您只想删除.0任何字符串的末尾但允许非整数值继续存在,您可以自己删除尾随文本:

class Test {
    public static void main(String[]args) {
        double d1 = 1234;
        double d2 = 1234.567;
        System.out.println(String.valueOf(d1).replaceFirst("\.0+$", ""));
        System.out.println(String.valueOf(d2).replaceFirst("\.0+$", ""));
    }
}

That snippet outputs:

该片段输出:

1234
1234.567

回答by anguyen

SONum = ""+cellSONum.getNumericCellValue().split(".")[0];

回答by Suresh Atta

Try with split().

尝试使用split().

         SONum = String.valueOf(cellSONum.getNumericCellValue());
         SONum  = SONum.split("\.")[0];

When you split 40002547.0with .,the split function returns two parts and the first one you need.

当你用 分割 40002547.0.,分割函数返回两个部分和你需要的第一个部分。

回答by Václav

If you want to be sure you are not cutting of any valid decimals, you can use regexp also:

如果你想确保你没有切割任何有效的小数,你也可以使用正则表达式:

String pattern = "\.0+"; // dot followed by any number of zeros
System.out.println(String.valueOf(cellSONum.getNumericCellValue()).replaceAll(pattern, "")); 

More on java regexp for example: http://www.vogella.com/articles/JavaRegularExpressions/article.html

更多关于 java regexp 例如:http: //www.vogella.com/articles/JavaRegularExpressions/article.html

回答by upog

try

尝试

double value = 23.0;
DecimalFormat df = new DecimalFormat("0.##");
System.out.println("bd value::"+ df.format(value))

回答by W vd L

As PaxDiablo also mentions, cellSONum.getNumericCellValue() returns a floating point.

正如 PaxDiablo 还提到的, cellSONum.getNumericCellValue() 返回一个浮点数。

You can cast this to Long or int to get rid of all behind the '.'

您可以将其强制转换为 Long 或 int 以摆脱 '.' 后面的所有内容。

String SONum = String.valueOf(cellSONum.getNumericCellValue().longValue());

used as example:

用作示例:

String SONum = String.valueOf((new Double(0.5)).longValue());

回答by Krullert

Consider using BigDecimal. You could simply say

考虑使用BigDecimal。你可以简单地说

BigDecimal scaledDecimal = new BigDecimal(value).setScale(0, RoundingMode.HALF_EVEN);

回答by messi

This will help in case your input is String and you need result also in String

如果您的输入是字符串并且您还需要字符串的结果,这将有所帮助

1). Convert the string to Double using Double.parseDouble,

1)。使用 Double.parseDouble 将字符串转换为 Double,

2). Type cast to int, then convert to string using String.valueOf()

2)。将类型转换为 int,然后使用 String.valueOf() 转换为字符串

private String formatText(String text) {
    try {
        return String.valueOf((int) Double.parseDouble(text));
    } catch (NumberFormatException e) {
        return text;
    }
}