1

I'm trying to floor a double data type number to two decimal places but I only get one instead when I enter a number like 150.

For example if I type this :

double num = 150.52152;

Math.floor(num * 100) / 100;

I get 150.52 and that's okay!

But If I enter double num = 150 I get 150.0 instead of 150.00

How do I solve this?

  • 4
    150.0 is the same as 150.00, it's just a matter of how the value is presented. You need to use a number formatter if you want a specific output – Joakim Danielson Mar 07 '21 at 14:49

4 Answers4

3

You could do it in two ways

  1. Using String.format
String.format("%.2f",floorValue)
  1. Using DecimalFormat
new DecimalFormat("#,##0.00").format(floorValue)

I recommend you store new DecimalFormat("#,##0.00") into a final variable and use it whenever required rather than creating a new DecimalFormat object every time formatting is required.

Demo:

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    public static void main(String[] args) {
        final NumberFormat format = new DecimalFormat("#,##0.00");// Do it once

        double floorValue = 150;
        System.out.println(format.format(floorValue));// Use it wherever you need to

        // Alternatively
        System.out.printf(String.format("%.2f", floorValue));
    }
}

Output:

150.00
150.00
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Gautham M
  • 4,816
  • 3
  • 15
  • 37
1

Controlling the number of decimal places is a function of converting the decimal to a String. Remember you are viewing a value in base 10 from base 2. Not every such value is representable (for example System.out.println(0.05+0.1);). Instead, use String formatting. Like,

System.out.printf("%.02f%n", 150.0);

or

System.out.printf("%.02f%n", 0.05 + 0.1);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you very much. I forgot to mention I want to print it in a return statemen like "Purchase value : " + num... How do I apply the same thing then? –  Mar 07 '21 at 14:56
  • `return String.format("Purchase value : %.02f", 150.0);` – Elliott Frisch Mar 07 '21 at 14:58
  • 1
    @PohovaniVodokotlic see this https://stackoverflow.com/help/someone-answers. – Dren Mar 07 '21 at 15:02
0

To display your awnser at all times with two values displayed after the decimal point you have two output methods. Your first is as follows:

System.out.printf("%.2f", <YOUR VALUE>);

%.2F calls two decimals in a float which is first defined by printf (print float) this cannot be removed as a integer cannot be a decimal.

Yours second option is to use a decimal format. For example:

// First define your fromat as decimalFormat
DecimalFormat decimalFormat = new DecimalFormat();
// Define number of digits after the decimal point in your case two
decimalFormat.setMaximumFractionDigits(2);
// Print out our result now formated in what we have just defined
System.out.println(decimalFormat.format(<YOUR VALUE>));

Although you output a string it can take a float (you may need to put "f" after your value to make sure it is defined).

Hope this is informative.

Rabbitminers
  • 315
  • 3
  • 11
0

You can also do it like this (but I would prefer String.format or System.out.printf). Note that trailing zeroes won't be displayed so 100.10230 would show as 100.1.

Generate some data

Random r =  new Random();
double[] dbls = r.doubles(10).map(d->d*1000).toArray();

Print the values

for (double d : dbls) {
    double df = (int)(d*100+.5)/100.;
    System.out.println(d + " --> " + df);
} 

Prints something similar to this.

738.2358190119878 --> 738.24
902.7752505591593 --> 902.78
467.26349962571953 --> 467.26
503.2049813704648 --> 503.2
20.288046322504805 --> 20.29
409.10379837674714 --> 409.1
559.2821881121522 --> 559.28
288.5513513499909 --> 288.55
124.07583289719682 --> 124.08
535.1799354550361 --> 535.18
WJS
  • 36,363
  • 4
  • 24
  • 39