Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiply to double values  RSS feed

 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everybody,


I want to multiply two double type values. That is ok with me,
But i am not able to get proper result

For example

405.123 * 155000.0 = expected result is 62794065

but i am getting something like this. 6.2794065E7


Please help....

Need immediate solution....

Thanks...
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me with this........
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Kanitkar wrote:Need immediate solution....

Ease up.
Aditya Kanitkar wrote:please help me with this........

Patience is a virtue

Now, about your problem. Double and float only have limited precision. Try using BigDecimal instead:
Output: 405.123 * 155000.0 = 62794065.0000
Those trailing zeros are scientifically correct; the number of decimals in any scientific calculation is the number of decimals of both operands added. To get rid of them use mult.stripTrailingZeros().
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Don't say anything is urgent. And take note of what I said in my private message.

As Rob has pointed out, you have obtained a correct result from that arithmetic. The problem seems to me that you are displaying it in a format you are unfamiliar with.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

first of all. My apologies to both of you.
It wont happen again.

Now, about my problem....

I agree that println is displaying it in correct format.
But i want to compare value in big decimal with another
value like this 7321542.00

I have also found a method in BigDecimal that is mult.doubleValue()

What should i do now...??

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BigDecimal class has constructors for creating new BigDecimal objects, and it has methods for comparing two numbers; using those, you should be able to achieve what you're asking.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am i missing something?
expected result is 62794065

but i am getting something like this. 6.2794065E7

Those numbers ARE the same. It's just a display issue.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Aditya Kanitkar wrote:Need immediate solution....

Ease up.
Aditya Kanitkar wrote:please help me with this........

Patience is a virtue

Now, about your problem. Double and float only have limited precision. Try using BigDecimal instead:
Output: 405.123 * 155000.0 = 62794065.0000
Those trailing zeros are scientifically correct; the number of decimals in any scientific calculation is the number of decimals of both operands added. To get rid of them use mult.stripTrailingZeros().


There is some problem with this method. stripTrailingZeros() because
it only works sometimes...
for example if i am getting the result of multiply method as : 374565245.000 (without)
it will work properly. But if i am getting the result like this... 2141236.2000
it does not returning a proper value... its like this.... 21412E61 something like this....

 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:am i missing something?
expected result is 62794065

but i am getting something like this. 6.2794065E7

Those numbers ARE the same. It's just a display issue.



Actually, when i use BigDecimal.floatValue method...... it gives this kind of value 6.2794065E7
but when i'm using multiply method of BigDecimal then gives 62794065.000 this kind of value.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still a display issue. If you want your data formatted in a particular way, then do that. Use a DecimalFormat object, don't just rely on the default formatting for the numbers.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If BigDecimal is using the scientific* formatting, use its toPlainString() method.

* The E stands for "times 10 to the power". So "6.2794065E7" means "6.2794065 times 10 to the power 7", or "6.2794065 times 10,000,000", or "62794065".
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The BigDecimal class has constructors for creating new BigDecimal objects, and it has methods for comparing two numbers; using those, you should be able to achieve what you're asking.



I'm almost done with my solution. But still there are some issues.
That is..
I'm using constructor like this

double d1=107.783,d2=21556.72d; //<--(Getting it from Database)
BigDecimal res=new BigDecimal(d1*d2);
So res contains result with no decimal point
Then, to put decimal point I'm having movePointLeft(2);
Thats all.
But problem is that.... right now in res the value is 23234.479517600000835955142974853515625 this.

So now what?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inaccuracy most likely originates from "d1*d2", which is a double and thus inexact. Try creating new BigDecimal(d1) and new BigDecimal(d2) and then multiply those.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, don't do any double arithmetic. Use two BigDecimal instances that contain the database values, then use those to multiply. If possible use a String to construct them; that way you determine the exact precision of the BigDecimal (namely that of the String). Use ResultSet.getString() instead of ResultSet.getDouble() for that.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out on windows calculator....

try
165000 * 104.453 = 17234745

expected answer is 172347.45
why doesn't windows calculator automatically add decimal point before 45??

I'm facing the same problem with myeclipse.
And when i try to divide the result by 100 then
sometimes it is not required to be divided but sometimes
it does......
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is anybody having any explanation for what i just asked in the above post....??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Kanitkar wrote:Check out on windows calculator....

try
165000 * 104.453 = 17234745

expected answer is 172347.45
why doesn't windows calculator automatically add decimal point before 45??


Because 165000 times 104.453 is 17234745.... and not 172347.45. Why don't you try it out for yourself on paper?

Henry
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i got it.... and i really dont have that much time to do that multiplication....

But in java... i'm getting the same result... as without decimal point.
So is like that... i've to divide the result with 100 everytime.... Is there any other solution
for this...

Because there are cases when you dont have to do such division with result.
There could be some straight forward solution...
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Kanitkar wrote: . . . i really dont have that much time to do that multiplication....
As my daughter would say, "Well, honestly!"

It hardly takes a second to work out, you are multiplying a number with 3 places after the decimal point by a number ending in 000, so you can tell the result will have no numbers after the decimal point.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!