1

This is the question and I am stuck that how can I append zeroes in place of empty spaces after the decimal in java. enter link description here

NmdvDisha
  • 57
  • 6

3 Answers3

1

A java.text.DecimalFormat instance can do this for you, here is an example:

new DecimalFormat("#,##0.00000").format(1.23);    // => 1.23000
new DecimalFormat("#,##0.00000").format(.987643); // => 0.98764
DuncG
  • 12,137
  • 2
  • 21
  • 33
1

You can use DecimalFormat#format to do so.

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

public class Main {
    public static void main(String[] args) {
        // Define the formatter
        NumberFormat formatter = new DecimalFormat("0.000000");

        // Tests
        System.out.println(formatter.format(0.3));
        System.out.println(formatter.format(123.3));
        System.out.println(formatter.format(0.335));
        System.out.println(formatter.format(0.0));
        System.out.println(formatter.format(1.0));
    }
}

Output:

0.300000
123.300000
0.335000
0.000000
1.000000
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Any string formatter can do it.

 String s = String.format("%.30f", 1.23);

or

 System.out.printf("%.30f %n", 1.23);

Those examples give you 30 places after the decimal point.

J.Backus
  • 1,441
  • 1
  • 6
  • 6