-2
package test;

public class mulcheck {

    public static void main(String[] args) {
        double  output = (125000 / (1-0.035));
        double finout = output * 1000;
        System.out.println(finout);
    }

}

output 1.2953367875647669E8

expected output 12,95,33,678.7564766

after multiplying the value received in output variable with 1000 instead of moving the decimal to the right it is giving above output

tried using float....

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    Take care of java naming conventions. Class names should start with upper case character – Jens Jan 02 '23 at 11:15

1 Answers1

1

Notice at the end 1.2953367875647669E8 its in exponential notation.

Use BigDecimal:

package test;

import java.math.BigDecimal;

public class mulcheck {

    public static void main(String[] args) {
        double  output = (125000 / (1-0.035));
        double finout = output * 1000;
        // System.out.println(finout);
        BigDecimal decimal = new BigDecimal(finout);
        System.out.println(decimal);
    }

}

Output :

129533678.75647668540477752685546875

Better Approach use printf for formatting the float value:

package test;

public class mulcheck {

    public static void main(String[] args) {
        double  output = (125000 / (1-0.035));
        double finout = output * 1000;
        System.out.printf("%.7f", finout); // Returns  7 decimal places
    }

}

Output:

129533678.7564767
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Udesh
  • 2,415
  • 2
  • 22
  • 32