0

I've seen a bunch of questions here on Stackoverflow referring to hiding the .0 after a double, but every single answer says to use DecimalFormat and do #.# to hide it. Except this is not what I want.

For every single possibility where the double does NOT end in simply .0, I want them to be how they are. Except when it DOES end in .0, remove it. In other words, I want to keep my precision at all times, unless it ends with a .0.

Examples:

0.0000000000000000042345470000230 -> 0.0000000000000000042345470000230
0.4395083451 -> 0.4395083451
46547453.00024235 -> 46547453.00024235

435.0 -> 435

Is there a way I can achieve this?

Further example:

This question here has the type of answer I am talking about:

Use DecimalFormat

double answer = 5.0;
DecimalFormat df = new DecimalFormat("###.#");
System.out.println(df.format(answer));

The ###.# above means that I am going to have the first 3 digits appear, a period, and then the first number after it. Regardless of my value, only the first fractional number will be formatted.

Community
  • 1
  • 1
  • Why the downvote? There is nothing off-topic about the question as it refers to programming. Also, there's no "minimal verifiable example" because I have no idea how to code this. –  Apr 02 '17 at 03:52
  • it is a problem about formatting and not about a specific data type. – Euclides Apr 02 '17 at 03:57
  • @HovercraftFullOfEels The examples are purely ***examples***. The stupidity of length of the numbers I provided are just extreme examples to show that they simply **do not** end with a `.0`. I know their not *actually* represented as is. –  Apr 02 '17 at 03:58
  • @Euclides As for the datatype, I added `double` as a tag, because it refers to formatting that specific dataype. Would you like me to change it? –  Apr 02 '17 at 03:58
  • You check for the exactly-zero case by checking `myDouble == (long) myDouble`, as long as the double is less than 2^53 (and if it's bigger, the fractional part is zero by definition). With that info, do you have enough to get the rest? – yshavit Apr 02 '17 at 04:02
  • @yshavit Ah, alright. I normally see casts to `int`, but I see what your getting at here. Thanks, lets see how I go. –  Apr 02 '17 at 04:04
  • @frayment it is not an issue about the data type per se, there is nothing wrong using double, but what are you trying to do? if you want to display 5.0 as 5 then you would type: if (5.0 % 1 == 0) then format it as just #. – Euclides Apr 02 '17 at 04:07
  • @Euclides I'm literally just trying to remove the trailing zeros, ONLY if theres nothing but zeros. If you look at yshavit's comment, whats the benefit of using modulo over casts, and vice versa? –  Apr 02 '17 at 04:09
  • You're assuming that floating point numbers can act like decimal numbers, and the truth is -- they can't. – Hovercraft Full Of Eels Apr 02 '17 at 04:13
  • @HovercraftFullOfEels I only want to make them act like one when I'm formatting string-wise though? –  Apr 02 '17 at 04:15
  • This is not how floating point numbers work. Seriously. – Hovercraft Full Of Eels Apr 02 '17 at 04:16
  • @HovercraftFullOfEels Of course not! :) I'm working on a library that does ridiculously accurate calculations, but can't stand having that `.0` printed when I have a whole number. –  Apr 02 '17 at 04:17
  • But **again** your concept of "whole" numbers has no meaning when it comes to floating point numbers. – Hovercraft Full Of Eels Apr 02 '17 at 04:19
  • @HovercraftFullOfEels Sorry, I don't understand what your trying to tell me? I know what a whole number is, and the logic behind floating-point numbers? –  Apr 02 '17 at 04:20
  • 1
    @frayment you are a bit confused, if you want to manage a double as an integer then you have the wrong approach. If you are using it for calculations then keep using the float numbers as they are. Then, if you need to print it into a report/user interface, then format it using some cool class as DecimalFormat. – Euclides Apr 02 '17 at 04:20
  • @Euclides But my question refers to not using `DecimalFormat` for the exact reason why someone would use it. If I use the `#.#` formatting, I only keep my precision to one decimal place! That is not what I am after. –  Apr 02 '17 at 04:22
  • You remain greatly confused about how floating point numbers work in not only Java but all digital computers. That's the crux of this question since in floating point numbers `.0` really has no meaning. – Hovercraft Full Of Eels Apr 02 '17 at 04:25
  • @HovercraftFullOfEels ... I understand that it has no meaning, because without it the fundamental meaning of a floating point number is lost. That is not my question. My question, is how, to remove the meaningless `.0` from a string! –  Apr 02 '17 at 04:27
  • 1
    Got it, it is time to start to use BigDecimal. – Euclides Apr 02 '17 at 04:27

1 Answers1

1

Well actually it's not complicated at all. Just check this (It even gives you the most precise number):

//Square root of 5 will give you a lot of decimals    
BigDecimal d1 = new BigDecimal(sqrt(5));
//5 will give you none
BigDecimal d2 = new BigDecimal(5);
//Print and enjoy
System.out.println(d1.stripTrailingZeros());
System.out.println(d2.stripTrailingZeros());

The stripTrailingZeros() will remove any trail of plain 0's, but keep the formatting if other numbers are present.

3vts
  • 778
  • 1
  • 12
  • 25
  • Oh my god, the precision on that thing. :O –  Apr 02 '17 at 04:34
  • Alright, I've just tried it, and have had to set the precision on it with `setScale` because the numbers I am using are so massively long. I used `d = d.setScale(10, RoundingMode.FLOOR);` on a number, and it is now equal to `500.0000000000`. I print this, and the trailing zeros are still there? –  Apr 02 '17 at 04:42
  • again, it is just a question of perception, print d.intValue() and you will obtain 500. – Euclides Apr 02 '17 at 04:47
  • @Euclides Yes but that is not what I am after. That would still remove my decimal trailings. I found the answer anyway, just use `skipTrailingZeros()`. –  Apr 02 '17 at 04:48
  • Great! I am glad you found it finally. – Euclides Apr 02 '17 at 04:52
  • is it skipTrailingZeros()? I have stripTrailingZeros(). – Euclides Apr 02 '17 at 04:55
  • stripTrailingZeros() in did – 3vts Apr 02 '17 at 04:57
  • Woops! stripTrailingZeros() it is! Did that one without looking. Thanks for the help guys! –  Apr 02 '17 at 05:00
  • @frayment I also found a library to read [Quadruple-precision floating-point format](https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format) that's the biggest precision you can get if you are interested... Just follow [this link](http://stackoverflow.com/a/21071907/3571692) to the answer I found – 3vts Apr 02 '17 at 22:02