How to prevent Double from turning a large number into exponential?

Asked

Viewed 156 times

-1

I have a system of calculations in my app, I use Textwatcher and when I calculate a large number saved, when I return the value, it comes in exponential (123e+23). I tried to use bigDecimal but it was the same mistake. Does anyone know what can be? I thank you already.

  • This is scientific notation, used exactly to show the number in a leaner way. Are you sure you want to show the number: 1230000000000000000000?

1 answer

3


One of the ways to do this is to use the String class itself.

String.format("%.0f", new BigDecimal("123e+23")); 

The output of this code is:

12300000000000000000000000

Another way using only the Bigdecimal class is:

new BigDecimal("123e+23").toPlainString();

Will produce the same output.

However, there must be a very specific case to show a number with as many digits for the user.

The number 123e+23 is in the scientific notation. It is the same as writing 123 x 10 23 or 1.23 x 10 25 or 1230000000000000000000.

  • So, almost worked, the problem is that I have to record in Double in the bank and when I convert this String to double back the exponential. But thanks, I’ll see what I can do here.

  • and if you do so: String.format("%.0f",Double.valueOf("1230000000000000000000"). Note that first I am turning a String into a Double (using valueOf). The result of this is a Double that is printed in scientific notation. After that, this Double is passed to the String format.

  • For me to record in need to use a "parseDouble", then comes back the scientific notation.

  • You’re saying that in your database is being recorded as scientific notation?

  • yes, when I go to record in the bank I have to use the parseDouble, then record with the scientific notation.

  • What kind of field is in your database?

  • in my bank, it’s real (sqlite) but I’m using getters and setters from a predefined library where it’s a Double for this attribute. I have to record as a double I can’t escape.

  • 2

    I think you’re making a mess. I don’t know how you are seeing this data recorded in the Sqlite, but probably the program you are using to see is making this conversion to scientific notation and you are getting the impression that the data was recorded like this.

  • Before recording I printei for test, just putting the parseDouble already gets the notation, After a certain number, 7 or 8 the Double uses scientific notation, I think I can do nothing, but vlw by help Cantoni.

  • 2

    sorry for the insistence on trying to help you.. I’m like this. : -) You see, you printed it in JAVA, but the data was recorded in Sqlite. It doesn’t mean that the data was saved as printed in Java. It doesn’t really matter. The important thing is that you, while recovering the data, format the representation if necessary.

Show 5 more comments

Browser other questions tagged

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