返回不带尾随零的双精度类型

huangapple go评论60阅读模式
英文:

Return double type without trialing zero

问题

我正试图返回双精度/浮点值,但不带有尾随添加的零。

我已经使用了NumberFormat、DecimalFormat,但一旦我将双精度或浮点数转换为结果,它就会添加尾随零(这是预期的)。我想知道是否有方法可以阻止这种情况。

private Double format(double input) {
    NumberFormat nf = NumberFormat.getIntegerInstance(Locale.US);
    Double result = null;
    BigDecimal bd = new BigDecimal(input);

    if (bd.intValue() > 99) {
        //(不想在这里添加尾随零)
        return (double) bd.intValue();
    } else {
        nf.setMinimumFractionDigits(1);
        nf.setMaximumFractionDigits(1);
        result = Double.parseDouble(nf.format(bd.doubleValue()));
    }
    return result;
}

附注:我在这里不是试图返回字符串值。

英文:

I am trying to return the double/float value but without the trailing zero added to it.

I've used NumberFormat, DecimalFormat but once I cast double or float to the result, it will add trailing zero(which is expected). I was wondering if there is a way to prevent this.

        private Double format (double input) {        

        NumberFormat nf = NumberFormat.getIntegerInstance(Locale.US);           
		Double result = null; 
        BigDecimal bd = new BigDecimal(input); 
        
        if(bd.intValue() > 99){
        //(don't want to add trailing zeros here)
				return (double)bd.intValue();
        }else{
            nf.setMinimumFractionDigits(1);
            nf.setMaximumFractionDigits(1);
            result = Double.parseDouble(nf.format(bd.doubleValue()));
        }
        return result; 

P.S. I am not trying to return String value here.

答案1

得分: 1

"尾随零" 只涉及浮点数或双精度数的字符串表示。

由于尾随零不影响数字的值,所以存储在浮点数或双精度中的数据保持不变。例如,"3.4" 和 "3.4000" 是同一个数字,它们只是这个数字的两种不同表示,就像 "3.4e+00" 仍然是显示同一数字的另一种方式。

英文:

The "trailing zeros" is only about the string representation of a float or double number.

As the trailing zeros do not affect the value of a number, the data stored in the float or double remains the same. For example, "3.4" and "3.4000" are the same number, it's only two different representations of this number, like "3.4 e+00" is still another way to display that very same number.

答案2

得分: 1

你可以使用Java中的内置方法**stripTrailingZeros**,该方法返回一个在去除尾部零后的BigDecimal

Double.valueOf(bd.stripTrailingZeros().toPlainString());
英文:

You can Use stripTrailingZeros which inbuilt method in Java that returns a BigDecimal after remove trailing zero.

Double.valueOf(bd.stripTrailingZeros().toPlainString());

答案3

得分: 0

如果将一个 int 值赋值给一个 double 变量,它默认情况下将始终以尾随零的形式表示。你有两个选择:

  1. 将其格式化为你想要的字符串。
  2. 将其转换为 BigDecimal,然后使用 BigDecimal#stripTrailingZeros 方法获取一个没有尾随零的 BigDecimal
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        double x = 5;
        System.out.println(x);

        BigDecimal y = BigDecimal.valueOf(x).stripTrailingZeros();
        System.out.println(y);
    }
}

输出:

5.0
5
英文:

If you assign an int value to a double variable, it will always be represented with a trailing zero by default. You have two choices:

  1. Format it into a string in whichever you want.

  2. Convert it into a BigDecimal and then use BigDecimal#stripTrailingZeros to get a BigDecimal without the trailing zero.

    import java.math.BigDecimal;
    
    public class Main {
    	public static void main(String[] args) {
    		double x = 5;
    		System.out.println(x);
    
    		BigDecimal y = BigDecimal.valueOf(x).stripTrailingZeros();
    		System.out.println(y);
    	}
    }
    

Output:

5.0
5

huangapple
  • 本文由 发表于 2020年8月19日 02:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63474699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定