• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to return double with two decimal points

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All ,

I have function that should return double with two decimal points.
I have string which already formatted two decimal points.

But I need to convert this string to double keeping the same two decimal points.

Double.parseDouble doesn't work as it always convert string 10.00 to 10.0 !
That doesn't work with me.

I need to have the same formatting with the returned double.

Note : I don't need to only display the value otherwise Double.parseDouble will work
I need to have the value as double to return it at my function

Any ideas ?



 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramy,
double is a primitive that doesn't specify an arbitrary precision. Take a look at BigDecimal which does what you need.
 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Ramy,
double is a primitive that doesn't specify an arbitrary precision. Take a look at BigDecimal which does what you need.



Thanks Jeanne.
But still doesn't work.

I have tried BigDecimal with setScale , the BigDecimal itself has the two digits but whenever I convert it to double still keeping one digit
Check below and advice please



 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that double doesn't support a fixed number of decimal points. You have to keep it as a BigDecimal and then convert directly to String for display. There is a decimal formatter class you can use to print a certain number of decimal places in a double. That doesn't it store it that way though. It just prints out X digits.
 
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The problem is that double doesn't support a fixed number of decimal points.



Ramy,

I agree with Jeanne and just want to expand on her advice. If your function needs to have a fixed precision (2 digits in this case) BigDecimal is the way to go for parsing the String.

However, if your function needs to return a double, as you have shown in your follow-up posting, then maybe I need to better understand the problem.

"10.00" (String) successfully parses to a double representing 1 x 10^1. You can return that, and you will have met the requirements of the problem as described. You may think you're incorrectly returning "10.0" because that's what you see when you convert it to a String and display it on the console. But actually it's more like +1.000000000000000E+001, which can be displayed in String form as "10", "10.0", "10.00", "10.000"....

I hope I haven't muddled things; I am saying that if the input was already limited to two digits of precision, and is less than roughly 10^14, your original solution is already working and you just may be misled by what System.out.printf() displays.

Scott
 
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… or more precisely 1.010×2³ in binary, since primitives are all denominated in binary.

Since it is a small whole number (or can be denominated in terms of powers of ½), 10 can be exactly defined as a double. Most numbers cannot be defined exactly like that, unfortunately.
 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to redesign the function to return string instead of double - as I can't reformat the double to have two decimal places.
That required me to change at jasper report to receive string instead of double - that it the step I was trying to avoid because expected integration issues with the client after deploying new jasper and jrxml files.

But unfortunately I had to do this step to avoid this double decimal Formatting limitations.

Thanks All.
 
Campbell Ritchie
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramy Nady wrote:I had to redesign the function to return string instead of double . . .

Very bad idea. Not only are there better ways to do it, which you have already been told about, but you are constraining the user about what the result of that method it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramy Nady wrote:I have tried BigDecimal with setScale , the BigDecimal itself has the two digits but whenever I convert it to double still keeping one digit


No it isn't. The display is.

A double (or Double) has no notion of how many "decimal points" it holds; it's simply a value - and often an inexact one at that (read this).
So there is no such thing as a "double with two decimal points".

I had to redesign the function to return string instead of double...


A really, REALLY bad idea.

You seem to be hung up on this notion of 2 decimal places, when in fact it's just a formatting issue.

If you want to display a double rounded to two decimal points, then use:
  System.out.printf("%.2f", 10.0);
or:
  System.out.printf("%.2f%n", 10.0);
if you want it to act like println().

Alternatively, you can use
  String.format("%.2f", 10.0);
to get the formatted String, and then just print that.

However, it is just possible that you need an exact value to 2 decimal places - for example, to hold an amount of money in dollars - and if that's the case then you have no alternative but to use BigDecimal (as you already did) and forget about doubles altogether.

But whatever you do, don't, don't, DON'T change the return type to a String just because it doesn't look right.

HIH

Winston
 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
If you want to display a double rounded to two decimal points, then use:

Winston



Please note that I don't want to display the value as I already indicated at my post.
I want to keep the double with two decimal points when returning from the function - which is impossible as everyone clarified.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramy Nady wrote:Please note that I don't want to display the value as I already indicated at my post.
I want to keep the double with two decimal points when returning from the function...


Can you explain why? It seems like an extraordinary requirement - or one written by someone who has no knowledge of Java.

Winston
 
lowercase baba
Posts: 13071
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it matter how many decimal places are in the return value? The only time you should care about the number of digits is when you USE it - whether you are displaying it, doing a calculation with it, or something else (although I'm not sure what else there is).

This statement makes no sense:

I have function that should return double with two decimal points.



Either it needs to return a double (in which case you have zero control over the number of digits), or you need to return <something> with exactly two - but again, you have not explained why.

It looks like you are passing this into a Jasper report (which I know nothing about), but wouldn't a solution be to change the report to display the number with the format required?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramy Nady wrote:
Please note that I don't want to display the value as I already indicated at my post.
I want to keep the double with two decimal points when returning from the function - which is impossible as everyone clarified.



The Jasper report is a display.
When we say "display" we mean anything intended for human consumption, or possibly for later import into some system with very picky formatting requirements.
 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,


Can you explain why?




Why does it matter how many decimal places are in the return value?



Since the jasper report already designed to have a double value field - will map with the same double value I need to return from my function.
The bug raised from our client to format this value with two decimal point formatting.

I was trying to keep the report as it is and change only in java code - Which I forced not to do and I have changed in the report jrxml itself finally !!


The Jasper report is a display.



I mean I don't want just to display using printf as Winston mentioned.


but wouldn't a solution be to change the report to display the number with the format required?



That what I did really - that required change in the report design itself.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramy Nady wrote:That what I did really - that required change in the report design itself.


Sounds like a much better idea to me.

There are cases where you need values stored to an exact number of decimal places, but in those cases you should be using BigDecimal, not double - but, just to re-iterate, NEVER a String.

Winston
 
Saloon Keeper
Posts: 26020
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pedantry time.

Floating point numbers have only ONE decimal point. That's the delineation (point) between the integral part of the number and the fractional part. The correct question would have been "how to return double with 2 decimal places. Meaning, 2 digits of fraction.

Of course, there's a fundamental problem with that. The IEEE floating-point format that Java uses has binary fractions, not decimal ones, so it would be impossible to return a "double with 2 decimal places" for the same reason that you can't return 1/3 to 2 decimal places. What you'd get back would be inaccurate.

Actually, the number of binary places for float and double values are fixed by their respective format definitions, not definable by the application programmer. They can be equated to an approximate number of decimal places, but the actual decimal precision varies depending on the numerical value, even if one disregards decimal/binary precision issues. So you get terms like "accurate to 14-1/2 digits".


As others have already said, if you want EXACTLY 2 decimal places, you need a non-floating number type such as BigNumber. On the other hand, if it's only the display value you care about, you can control that using output formatting options.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic