Home > database >  java how to delete digits after comma in double data type
java how to delete digits after comma in double data type

Time:05-29

so for example what I have and what I want it to, first numbers are double and I would like them to be integer but with no zeros


2.00 -> 2


5.012 -> 5


I tried with this, but still doesn't work

if(result % 1 == 0){
    String temp = String.valueOf(result);
    int tempInt = Integer.parseInt(temp);
    tekst.setText(String.valueOf(tempInt));

    }else {
     tekst.setText(String.valueOf(result));
     num1 = 0;
     num2 = 0;
     result = 0;
}

result variable is double, also this shows after compiling

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "887.0"

CodePudding user response:

There is a caveat that you need to be aware about since you're trying to use an intermediate int variable:

  • The range of double values is far broader than the range of long (and int obviously). So by converting a double into a long and then again to double you might lose the data.

Here are some ways how it can be done without losing the data:

1. Modular division:

double num = 59.012;

double wholeNum2 = num - num % 1;

2. Static method Math.floor():

double num = 59.012;

double wholeNum = Math.floor(num);

3. DecimalFormat class, that allow to specify a string pattern and format a number accordingly:

double num = 59.012;

NumberFormat format = new DecimalFormat("0");
double wholeNum = Double.parseDouble(format.format(num)); // parsing the formatted string

All examples above will give you the output 59.0 is you print the variable wholeNum.

When you need to obtain a double value as a result with the fractional part dropped, options 1 and 2 are preferable. But a string representing this number will still contain a dot and one zero .0 at the end.

But if you need to get the result as a String containing only the integer part of a double number, then DecimalFormat (as well String.format() that was mentioned in the comments) will help you to get rid of the fractional part completely.

NumberFormat format = new DecimalFormat("0");

System.out.println(format.format(59.012));

Output:

59

CodePudding user response:

This is what i would do.

double num1 = 2.3;
double num2 = 4.5;

Later...

    int num11 = (int) num1;
    int num22 = (int) num2;
    System.out.println(num11   ", "   num22   ".");

The result would be:

2, 4.

That's all.

CodePudding user response:

use (Integer)(Math.floor(insert your double here)) The floor(double x) method is already built into the math class, and returns a double, for instance 5.5 becomes 5.0 Then we just instance that number as an Integer (or alternatively an int) and we get out result

CodePudding user response:

Are you maybe parsing a double string so 10.0001 with an int parse? Because it will recognize that it's not an int and throw an error. You should first parse it to a double and then cast that to an int.

    //if you want to parse it from a string
    String doubleString = "100.0001";

    double parsedDoubleValue = Double.parseDouble(doubleString);
    System.out.println("Parsed double value: "   parsedDoubleValue);
    
    int castedValue = (int) parsedDoubleValue;
    System.out.println("Casted double value to int: "   castedValue);
    
    //if you have to original double
    double doubleValue = 1.0001;
    int intValue = (int)doubleValue;

    System.out.println(intValue);

CodePudding user response:

According to the method documentation for parseInt(String s):

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign ' ' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(String, int) method.
Params:
s – a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException – if the string does not contain a parsable integer.

The number is a double and contains characters that are not parsable ie"." so it thows error.

You may consider:

int tempInt = Integer.parseInt(temp.split("\\.")[0]);

or just directly skip parsing the Integer:

tekst.setText(temp.split("\\.")[0]);
  • Related