-2

For example:

double amount = 9.090909090909097E-4;

amount = getRoundedNumber(amount, 4);

public static double getRoundedNumber(double number, int places) {

    double multiplier = Math.pow(10, places);

    return (double) Math.round(number * multiplier) / multiplier;
}

the result is : 9.0E-4
I need it to be in regular numbers because I am sending it to our db(backend) in json format:

 "amount":9.0E-4


how can I make it 0.0009?

NicoTing
  • 83
  • 8
  • 4
    It _is_ in real numbers. Your database will cope just fine, if you pass it as a `double`. – Dawood ibn Kareem May 17 '22 at 03:57
  • @DawoodibnKareem Updated the question, sorry I forgot to mention that I am sending the details to our db in json format. – NicoTing May 17 '22 at 04:02
  • 1
    Possible duplicates! https://stackoverflow.com/questions/13064567/java-beginner-converting-scientific-notation-to-decimal#:~:text=You%20can%20do%20it%20like,setMinimumFractionDigits(7)%3B%20System. – Mohamad Ghaith Alzin May 17 '22 at 04:16
  • 3
    Any JSON formatter library should parse that number correctly. Whatever you do, don't try to do your own JSON formatting or parsing. All these niggly little issues with JSON have been solved already, by the people who make these libraries. – Dawood ibn Kareem May 17 '22 at 04:18
  • Possible duplicate: [Round a number to n decimal places](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java). – user207421 May 17 '22 at 04:39

2 Answers2

4

Scientific notation is a valid representation for floating point ("real") numbers. Furthermore, JSON allows numbers to be represented in scientific notation; see the JSON syntax graphs on json.org.

There is no need to round the number or convert to not use scientific notation ... to make it acceptable in JSON format.

  • If you create the JSON using one of the many Java JSON parser / unparser libraries, they will take care of the formatting.

  • If you are formatting the JSON by hand, then Double.toString(double) will produce an acceptable rendering for your JSON.

  • If you insist on avoiding scientific notation, there are a variety of ways to do it. A simple way is String.format("%f", value). Others are described in How do I print a double value without scientific notation using Java?.

However, beware of rounding. The string 0.0009 is NOT a valid representation of 9.090909090909097E-4. You have discarded roughly 13 decimal digits of precision. The former differs from the latter by almost 10%.

In short, while 0.0009 looks nice, it is mathematically very, very wrong.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

This using the NumberFormat class https://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html

output: 0.00091

import java.util.*;
import java.text.*;

public class Main {
    public static void main(String[] args) {
        double amount = 9.090909090909097E-4;
        
        NumberFormat formatter = new DecimalFormat("###.#####");  
        String amountStr = formatter.format(amount);
        
        System.out.println(amountStr);
    }
}

Ref:

https://stackoverflow.com/questions/13064567/java-beginner-converting-scientific-notation-to-decimal#:~:text=You%20can%20do%20it%20like,setMinimumFractionDigits(7)%3B%20System