-1

I am trying to take a Double variable called 'startCheckNumber' which should have a value of '40305555' and convert to String. In doing a debug of my code the startCheckNumber shows a value of 4.030555E7. If I do the following command to convert to String it shows it like that instead of '4030555'

   String displayCheckNumber = String.valueOf(startCheckNumber)   ;

Is there a better way to convert a double variable to String in this case than using ValueOf? I tried 'Double.toString(number)' format and that didn't work right

Thanks

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
hammerva
  • 129
  • 1
  • 14
  • 2
    Do you want to do it in Javascript or Java? – Jagdish Idhate Aug 12 '16 at 15:00
  • You could try `NumberFormat.getNumberInstance().format(startCheckNumber)` or `String.format("%f", startCheckNumber );`. – Thomas Aug 12 '16 at 15:04
  • 1
    Is a duplicate of [*How to print double value without scientific notation using Java?*](http://stackoverflow.com/questions/16098046/how-to-print-double-value-without-scientific-notation-using-java). Is not a duplicate of [*Converting double to string*](http://stackoverflow.com/questions/5766318/converting-double-to-string) (the answers there just say to do what's above). – T.J. Crowder Aug 12 '16 at 15:07

1 Answers1

2

Sorry I should have looked harder. I found and this seemed to work

   DecimalFormat decimalFormat = new DecimalFormat("#");
   String displayCheckNumber = decimalFormat.format(startCheckNumber);
hammerva
  • 129
  • 1
  • 14
  • I was absolutely sure the code in your question wouldn't produce the string you showed () until [I tried it](http://ideone.com/ruiykp). Talk about surprising. I mean, yes, 4.030555E7 == 40305555, they're just in different notations, but... – T.J. Crowder Aug 12 '16 at 15:05
  • @T.J.Crowder 4.030555E7 != 40305555 Notice the different number of 5's – FredK Aug 12 '16 at 15:37
  • @FredK: That's just a typo in the question (what I get for copy and paste from there rather than the linked output). The actual output has all of the 5s in the scientific notation. – T.J. Crowder Aug 12 '16 at 15:48