Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1)round(1580/11,4) = 143.6364. 2) round(143.6364*11,8) = 1580.00040000

How can I get 1580 back. I want 4 digits precision in decimal case 1) and 8 digits precision in case 2). No truncating allowed.

Please help. Thanx in advance.
Posted
Comments
Rob Branaghan 28-Apr-11 8:33am    
Can I just ask why you want to round it to 4 decimal places, then do it back the other way to 8?
Sergey Alexandrovich Kryukov 28-Apr-11 14:38pm    
OP commented:

@MRB :I am not confused at all..
Let me explain

@Rob@Love2Code : Bcoz 11 is unit, 1580 is dealvalue, 143.6364 is quantity.

Suppose i save it to database. So in the update mode i get only the truncated result.

I have scenarios where i need to calculate dealvalue and quantity back and forth.

The user allows only 4 digits decimal precision for quantity and 8 for deal value.

Hope its clear now.

@all others, i need to check out ur solutions. will reply after that. Thanx all

When you round like that, your first rounding needs to have a precision of at least 1 digit greater than the reverse rounding.

Example:

C#
var res = Math.Round(1580d / 11d, 9);
var outVal = Math.Round(res * 11, 8);


That's C# syntax, but it should be similar to VB.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Apr-11 8:48am    
Nice solution. 5+
Nish Nishant 28-Apr-11 8:57am    
Thank you!
rehnam 28-Apr-11 9:04am    
if i use var res = 1580.12345678D / 11D, then too i'll get the same result. But i want "res" to be rounded to 4 digits. Thats what get saved to database. And in update mode, i get the rounded value only.
Nish Nishant 28-Apr-11 9:07am    
Well then when you round back, round back to 4 digits. You cannot get back data that you've lost.
Manfred Rudolf Bihy 28-Apr-11 15:03pm    
Very true indeed!
(But it seems to be hard concept to grasp!) :)

Assuming you KNOW that you started with an integer . . .

C#
double a = System.Math.Round(1580/11.0, 4);
double Delta = 0.00001;
int b = (int)(System.Math.Floor((a * 11.0) + Delta));


In other words, do your multiplication by 11 - it won't come out as an exact integer (floating point arithmetic rarely does) - add a small amount to make sure you are just over, not just under, an integer - and take Floor to get next lower integer.

[sorry, this is C# but the functions / techniques are the same in VB, just change the syntax]

If you didn't start with an integer, you need to know how many decimal places you had - and scale values accordingly - so if 3dp (e.g. 1580.123) do:

C#
double a = System.Math.Round(1580.123/11.0, 4);
double Delta = 0.00001;
double b = System.Math.Floor((a * 1000 * 11.0) + Delta) / 1000;



If you've rounded your intermediate result to 4dp but know you started with a value that wasn't necessarily integral and may have had up to 8 valid dp, you CAN'T, even theoretically, retrieve the original value accurately to 8dp.

e.g. 1580.00000001 and 1580.00000002 come out the same if divided by 11 and then rounded to 4dp - so you cannot distinguish between them when reversing the operation, so one of them must come out 'wrong'.
 
Share this answer
 
Comments
NuttingCDEF 28-Apr-11 8:44am    
See other solution "first rounding needs to have a precision of at least 1 digit greater than the reverse rounding" - agreed - my last section is effectively saying something similar.
Manfred Rudolf Bihy 28-Apr-11 8:47am    
Good answer! 5+
rehnam 28-Apr-11 8:58am    
Starting is not supposed to be an integer. if the val is 1580.12345678, i need to get it back too.
NuttingCDEF 28-Apr-11 9:03am    
So:

(a) You need to know what precision you need to get back to
(b) You must not round the intermediate result too far or you won't be able to get back

If you NEED an intermediate result that is presented to some specific accuracy, you need to use both the rounded and unrounded intermediate values - one for presentation, the other to have a route back to your starting point. See my final 'e.g.'
Sandeep Mewara 28-Apr-11 9:35am    
My 5! Good answer.
You seem to be confused. The result of your 1st operation is rounded to 4 decimal places and you really want to have the result of the 2nd operation which depends on the first to have a precision of 8 decimal places. That doesn't really make any sens, but you could try to explain why you are doing this.
As to the result not being equal to 1580. The result of the first operation was rounded to four decimal places when it was really a periodic decimal fraction:[^] .63636363 etc.. So once you start rounding you'll most likely won't get the same result where you started from. This is not an error but inherent to the problems encountered when doing rounding.
What you can do is separate GUI state from you logic. You can always do rounding when displaying the results, but keep the original result for doing further calculations.

Regards,

-MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Apr-11 14:47pm    
This is correct (my 5). I suspect OP has much basic confusion. Who needs rounding, after all? I think all OP needs is numeric formatting.
Please see my answer.
--SA
Manfred Rudolf Bihy 28-Apr-11 15:02pm    
Thanks SA!
Sergey Alexandrovich Kryukov 28-Apr-11 15:59pm    
I understand you already recommeded to focus on "displaying the results". Essentially, I only add an assumption that the prior part of concerns can be just skipped.
--SA
Sergey Alexandrovich Kryukov 29-Apr-11 2:27am    
By the way replied that he is not confused and needs rounding. I still cannot see why. "Has to follow certain rules" does not satisfy me.
--SA
Manfred Rudolf Bihy 29-Apr-11 8:46am    
If you only knew of the strange requirements some of our customers are bugging us with, you'd gladly jump into a pool of molten lava whilst loudly proclaiming that you are thoroughly enjoying yourself! :))
I still think you're confused.

You're confused when you say "I want it rounded". I know almost no cases when anyone needs anything rounded. Rounding introduces "rounding errors"; and this is a very delicate matter: rounding errors, even though each one is acceptable, can accumulate, and so on.

I think you need the rounded "output", that is, only string presentation of the value should be rounded. This is completely different thing.
All you need to know is formatting. Please see how string.Format and object.ToString methods work with formatting.
See:
http://msdn.microsoft.com/en-us/library/txafckwd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Apr-11 15:02pm    
Agreed, SA! 5+
That pretty much wraps it up what I tried to express in the last sentence of my solution.
Sergey Alexandrovich Kryukov 28-Apr-11 15:56pm    
Thank you, Manfred.
--SA
Nish Nishant 28-Apr-11 15:37pm    
My 5 too. Good answer.
Sergey Alexandrovich Kryukov 28-Apr-11 15:57pm    
Thank you, Nishant.
--SA
rehnam 29-Apr-11 0:42am    
@SAKryukov: I think you want me to think that i am confused. My problem is not how to display the values. I am well aware of format functions. My problem is with the rounding part itself, as the users has to follow certain rules (4 dp for qty and 8 dp for dealvalue). Thank tou all for ur comments anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900