How to take only the number after the comma

Asked

Viewed 5,579 times

5

When I do the 162/11 calculation, the result obtained is 14,72727272727273. But when I use the code System.out.printf("%.1f", teste);, the number is displayed 14,8.

I’d like a way to keep just the 8 in one int.

2 answers

5


Why not do that?

double teste = 162.0 / 11.0;
System.out.printf("%d", ((int) Math.ceil(teste * 10)) % 10);

The idea here is:

  1. Multiply by 10 so that the first digit after the comma becomes the last digit before it.

  2. The Math.ceil round the number up. So, your digit 7 becomes an 8.

  3. The cast for int cut every digit after the one you’re interested in.

  4. The rest of the division by 10 cut all digits before the one you’re interested in.

As a result, you will get an integer number from 0 to 9 that corresponds to the digit you want and nothing else besides.

In addition, there is a very interesting advantage: This is all solved only with math without you having to use Strings at no time.

See here working on ideone.

If you want to round down instead of up, just replace the ceil for floor. If you want to round up to the nearest integer, then you can use rint.

  • Dude, I figured he wanted to round it up because it’s the closest number of integer it would have, you could use an if and test if the mod is less than 0.5 and in this case round it down.

  • 1

    @pmargreff I understood that he wanted to round up because otherwise, 14.7272... would be rounded to 14.7 instead of 14.8. However, if the idea is to round down, just use the floor instead of ceil. If the idea is to round to the nearest integer, just use rint.

  • Cool, I didn’t know about the rint : )

2

For the value that follows after the comma take only the part after the comma (works with positive and negative):

double valor_decimal = valor - (int)(valor);

Or, if you prefer the arredontamento do:

String valorString String.format("%.1f", valor);
double valorArredondado = Double.parseDouble(valorString);

Then if you want to take only one number after the comma do:

int valor final = (int)valor_decimal * 10;

If you want to format for all numbers after the comma you can try to convert to string, and multiply the size returned by the function split:

String[] divisor = valor_decimal.toString().split("\\,");
int valor final = (int)valor_decimal * divisor[1].length();

The problem of this second solution is that it can burst the representation of an integer, to circumvent this you can use a Biginteger to ensure that it does not happen!


References:

SO en - How to get the Numbers after the decimal point?

SO en - Number of decimal digits in a double

  • Hello, it worked well, the problem is that with the function System.out.printf("%.1f", teste); it returns 8 and from this method it returns 7, I would need the 8. There is no way to use ("%.1f", teste) to format a variable ??

  • Amended answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.