This discussion is archived
7 Replies Latest reply: Mar 11, 2013 10:13 AM by rp0428 RSS

String.format cuts my float and adds 0's

995898 Newbie
Currently Being Moderated
I have a double with a decimal length of 40+. I'm using String.format with the format code of:
%2$39.32d (2 being the index number of the variable) and when I run the program it 'cuts' my double (ie 32.3232323232323232323232323232323232etc...)
to read 32.3232323223230000000000000. It is padding with zero's when there is a value there. Any ideas why and how I can avoid this.

Thank you
  • 1. Re: String.format cuts my float and adds 0's
    gimbal2 Guru
    Currently Being Moderated
    40+ decimal places? As far as I know that won't fit into a double. You need to be using BigDecimal.
  • 2. Re: String.format cuts my float and adds 0's
    rp0428 Guru
    Currently Being Moderated
    >
    I have a double with a decimal length of 40+.
    >
    No you don't - not unless ALL of those trailing digits are zero that lie beyond the precision capabilities of a 'double'.

    Post the code you are using so we can see what the datatypes really are and explain what you are doing.
  • 3. Re: String.format cuts my float and adds 0's
    EJP Guru
    Currently Being Moderated
    I have a double with a decimal length of 40+.
    You have a double with 53 binary digits of mantissa just like evey other double. This is 15.9 decimal digits. You can write more trailing decimal digits in a Java double literal, but they get truncated on the way in, by the compiler, not on the way out by decimal formatting.
  • 4. Re: String.format cuts my float and adds 0's
    995898 Newbie
    Currently Being Moderated
    Thanks for BigDecimal, I tried it out (always learning about something new), and it definitely gives me 32 non-truncated decimal digits, but I think it's from the next storage in memory. Someone else mentioned how double is truncated on the way in, not on the way out. So, I'm gonna go with double for now before I change everything over to BigDecimal, see if the program is effective then decide if BigDecimal lis the way to go or not (slows down the program, no)? So cool though, thank you!


    Also please excuse me as I'm new to this forum, I would have entered correct on the both of you if I had known I could only choose between helpful and correct. The two of you helped me a lot! This is a great forum and I look forward to posting more questions! (As there will be more questions)!
    Edited by: 992895 on Mar 11, 2013 8:53 AM

    Edited by: 992895 on Mar 11, 2013 8:55 AM
  • 5. Re: String.format cuts my float and adds 0's
    Kayaman Guru
    Currently Being Moderated
    992895 wrote:
    and it definitely gives me 32 non-truncated decimal digits
    Of course, it gives you arbitrary precision.
    but I think it's from the next storage in memory.
    Well obviously more precision means more memory.
    So, I'm gonna go with double for now before I change everything over to BigDecimal, see if the program is effective
    There are certain situations where BigDecimal is required, such as when you're dealing with money. However, often double is enough.
    then decide if BigDecimal lis the way to go or not (slows down the program, no)?
    The decision between using double and BigDecimal is not about performance. Also unless you're doing heavy calculations I doubt you'd notice the difference.
    This is a great forum and I look forward to posting more questions! (As there will be more questions)!
    In that case welcome to the forums!
  • 6. Re: String.format cuts my float and adds 0's
    gimbal2 Guru
    Currently Being Moderated
    (slows down the program, no)
    We're not using 386 machines anymore. Computers have plenty of CPU horsepower to do these trivial things hundreds of thousands of times a second.
  • 7. Re: String.format cuts my float and adds 0's
    rp0428 Guru
    Currently Being Moderated
    >
    So, I'm gonna go with double for now before I change everything over to BigDecimal, see if the program is effective then decide if BigDecimal lis the way to go or not (slows down the program, no)?
    >
    The first question you need to answer is whether you want to keep all of those decimal digits or not.

    If you don't then you can use 'double'; if you do then you CANNOT use 'double'.

    The next question you need to answer is what you plan to do with that data once you get it into Java. For example, if you planned to load the data into Oracle then you could use TO_NUMBER (myStringWith40Digits) on the value and you would not lose as many of those decimal digits. For that use case you would use your string and not use 'double' or 'BigDecimal' at all.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points