Friday, 15 July 2011

string - Java : How to count the number of decimals contained in a double value that represent money? -


A change made application, created with Java, I'm facing a problem number of decimal in a double value Calculate for a while.

Double values ​​represent money, so all my operational results should be done, which can be stored as a double value 12.5797886 in the form of 12.587886 after an operation at the original price. To counter this effect, I chose to use the string, such as:

  string amount string = string.value (amount); String decimal = sum string. Substring (amountString.indexOf ('.') + 1);  

So if the amount is 12.58, I get 58 and not 57,978 ...

My problem only changes to 12849561476651.2501535 in such a way as to the large amount , Scientific notation while stored in a double then double the value of the amount 1.284956147665125E13

Unfortunately I can not count just after the dot dot and position 13. Position is just this one where the original After the kind of decimation everything will take 2501535 one 0 to the third place, on After Nam position 25 goals to 13.

I do not find any solution for people who want to print decimal values ​​without scientific notation, they use .printf (% F, ...) or something similar.

But, if this is possible, how will I fill the number of decades contained in double values ​​and will take care of storage problems and scientific notation?

I should know whether it is more than 2 decimals In addition, if you have a beautiful display solution, even if it is a complex or strange one, it would be great.

Thanks

Do not use double or float to store the currency , Or to store any decimal number that should be accurate, because double and float can be wrong.

You can use BigDescam, or you can use an int or long and keep track of your decimal point.

For more information on this, see "Item 48: section", "Must be effective in bloch" by Java:


No comments:

Post a Comment