0

I have a function that accepts only a double value, I want that double value to appear with trailing zeros, like when I pass 100.00 I want it to be displayed 100.00 not 100.0,

I don't want to use anything to do with system.out.printf() since I want it as a double value to pass it the function

P.S: I cant update the function since it not mine its imported from a package. please help

Hadi Nasser
  • 31
  • 1
  • 2
  • are you familiar with java.text.DecimalFormat? – Ralph Aouad Oct 06 '20 at 09:22
  • 8
    There's no "number of trailing zeroes" setting on double primitives nor on Double objects if that's what you are asking. 100.00 and 100.0 (and 100) are the same double value, they're just varying string representation – Aaron Oct 06 '20 at 09:22
  • Do you have any control over how the double is printed in the function you are passing the double to, or can you not change that function at all? By your description it looks like you don't so you will have to live with that function printing double values without trailing zeroes. – OH GOD SPIDERS Oct 06 '20 at 09:23
  • 1
    Does this answer your question? [Round a double to 2 decimal places](https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places) – Benjamin Zach Oct 06 '20 at 09:24
  • 1
    To rephrase my previous comment : you can't create a double that is 100.00. You can create a double that is equal to 100, and you can get a string representation of it that has two trailing digits, but you can't pass 100.00 to a function expecting a double. – Aaron Oct 06 '20 at 09:27
  • a Double having an infinite number of trailing zero is the same as a Double with none. Don't confuse mathematical representation and string (human readable) representation – jhamon Oct 06 '20 at 09:35
  • I know that , that's why I am struggling with it, its an online package and need this format for double values, thank you for your help ,I will try to contact them – Hadi Nasser Oct 06 '20 at 09:38
  • 1
    If it take a `double` value, it can't need any formatting. That's not possible. If it takes a `String` representing a double value, then see Benjamin Zach's comment – jhamon Oct 06 '20 at 09:44

0 Answers0