take only the decimal of a double

Asked

Viewed 295 times

1

How do I enjoy just the decimal of a double?

I’m trying to print the decimal part of a number in monetary unit, R$ 102,33 -> I want to print only thirty-three cents.

At the moment my code is like this:

public decimal(double x)
{
    x = 102.33;
}

4 answers

1

try it this way, I think it works!

public double decimal(double num){
    return num % 1; //retorna o resto da divisao
}

OUUUU

public decimal(double x){
    resto = x % 1; //mesma coisa de antes
}

as I don’t know if you are doing a common method or the constructor method so I did both.

1

You can multiply the value by 10^n, in which n is the number of decimal places you want to show. Then simply subtract from that value the entire part, which is ((int) x) * 10^n.

In your case, with n = 2 and x = 102.33, x*10^n = 10233.00 and ((int) x)*10^n = 10200.00. After making the subtraction x*10^n - ((int) x)*10^n, you will have 33.00 as value and just convert to whole.

So your code would look that way:

public void printDecimal(double x, int n){
  double p = Math.pow(10.0, n);
  int dec = (int) (x*p - ((int) x)*p);
  System.out.println(dec);
}

or if you want to return the value:

public int decimal(double x, int n){
  double p = Math.pow(10.0, n);
  return (int) (x*p - ((int) x)*p);
}

If you want the value in the format 0.33, just divide by 10^n. Note that you could simply not multiply by 10^n, but it would have accuracy problems and the output to 102.33 would be something like 0.3299999999999983. To avoid this problem, we can multiply by 10^n, perform operations with integer values and then split by 10^n.

public static void printDecimal(double x, int n){
  double p = Math.pow(10.0, n);
  int dec = (int) (x*p - ((int) x)*p);
  System.out.println(dec/p);
}

0

You can try a simple conversion: (int) num There are also the methods Math:

Math.floor(num): returns a int with the value of double rounded down;

Math.ceil(num): same thing, but rounded up;

Math.round(num): picks up the int closest to the double chosen (For example, the value 0.9 returns 1, while 0.4 returns 0).

After that you can make a simple subtraction. Example:

double decimal = (num - Math.floor(num));

It is possible to do the same by extracting the rest of the division using %:

double decimal = (num % 1);

Or, if you are a little curious, and like to do things the hard way (and heavy, slow, etc.), you can use a loop of repetition, as in the example below:

while(num>1){
  // cálculo pra extrair o número
  num--;
}

It depends a lot on your goal. If it is for mere didactic purposes, I advise to try all three.

0

You can also use Regex to extract this value

public int decimal(double num) {
    Pattern pattern = Pattern.compile("(?<=[\\.])[0-9]+");
    Matcher matcher = pattern.matcher(Double.toString(num));
    matcher.matches();
    return Integer.parseInt(matcher.group(0));
}
  • Is there somewhere I can study Regex?

  • It has some legal guides like this one from https://medium.com/trainingcenter/understandingde-uma-vez-por-todas-expressions-regulares-parte-1-introduction-dfe63e289dc3 and sites like https://www.debuggex.com that draw the diagram of your regex where you write @andrecatete

Browser other questions tagged

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