How to remove decimal values from a value of type 'double' in Java

后端 未结 19 1777
孤独总比滥情好
孤独总比滥情好 2020-12-08 09:26

I am invoking a method called \"calculateStampDuty\", which will return the amount of stamp duty to be paid on a property. The percentage calculation works fine, and returns

相关标签:
19条回答
  • 2020-12-08 10:00

    I did this to remove the decimal places from the double value

    new DecimalFormat("#").format(100.0);
    

    The output of the above is

    100

    0 讨论(0)
  • 2020-12-08 10:01

    Nice and simple. Add this snippet in whatever you're outputting to:

    String.format("%.0f", percentageValue)
    
    0 讨论(0)
  • 2020-12-08 10:01

    Type casting to integer may create problem but even long type can not hold every bit of double after narrowing down to decimal places. If you know your values will never exceed Long.MAX_VALUE value, this might be a clean solution.

    So use the following with the above known risk.

    double mValue = 1234567890.123456;
    long mStrippedValue = new Double(mValue).longValue();
    
    0 讨论(0)
  • 2020-12-08 10:01
        public class RemoveDecimalPoint{
    
             public static void main(String []args){
                System.out.println(""+ removePoint(250022005.60));
             }
     
           public static String  removePoint(double number) {        
                long x = (long) number;
                return x+"";
            }
    
        }
    
    0 讨论(0)
  • 2020-12-08 10:02

    You could use

    String newValue = Integer.toString((int)percentageValue);
    

    Or

    String newValue = Double.toString(Math.floor(percentageValue));
    
    0 讨论(0)
  • 2020-12-08 10:04

    You can convert the double value into a int value. int x = (int) y where y is your double variable. Then, printing x does not give decimal places (15000 instead of 15000.0).

    0 讨论(0)
提交回复
热议问题