|
-
Oct 25th, 2011, 06:01 AM
#1
Thread Starter
Frenzied Member
Round Values
Hello
I have an application that need to round some values, with a normal symetric round, but the options available in the round function doesn't allow me to do this kind of round.
For example, if i have
vb.net Code:
?Math.Round(0.275,2)
0.28
?Math.Round(1.275,2)
1.27
?Math.Round(1.275,2,MidpointRounding.AwayFromZero)
1.27
?Math.Round(1.275,2,MidpointRounding.ToEven)
1.27
The only way that looks like that i have a symmetric round it's when i use the format function:
vb.net Code:
?Format(0.275,"N2")
"0.28"
?Format(1.275,"N2")
"1.28"
I read some info available at msdn, i understand the bankers round technique, but when using the midpoint rounding the round should be ok, and it isn't. I'm missing something here?
Can i use the Format function and by using the Nx string, i have a symmetric rounding always?
Thanks
Meanwhile i have this function to do the job:
vb.net Code:
Public Function MyRound(ByVal valor As Decimal, ByVal casasDecimais As Integer) As Decimal
Dim sinal As Integer = Math.Sign(valor)
Dim escala As Decimal = Math.Pow(10, casasDecimais)
Dim arredondado As Decimal = Math.Floor(Math.Abs(valor) * escala + 0.5D)
Return ((sinal * arredondado) / escala)
End Function
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Oct 25th, 2011, 06:56 AM
#2
Re: Round Values
Hi,
The Math.Round method works on Decimal type values, but you are passing it Doubles in your examples
vb Code:
?Math.Round(CDec(0.275), 2) '0.28 ?Math.Round(CDec(1.275), 2) '1.28 ?Math.Round(CDec(1.275), 2, MidpointRounding.AwayFromZero) '1.28 ?Math.Round(CDec(1.275), 2, MidpointRounding.ToEven) '1.28
Turning Option Strict On is a good idea. It will show some type conversion errors in your own round function, but unfortunately won't pick up the problem when you pass in literal values.
EDIT
Strike that. It of course takes Doubles, so I have no idea why it is rounding like it does in your examples.
Last edited by Inferrd; Oct 25th, 2011 at 07:08 AM.
-
Oct 25th, 2011, 11:50 AM
#3
Re: Round Values
A little more digging reveals that the problem is the precision with which floating point numbers are stored in Double type variables.
Try:
MessageBox.Show("1.275 is stored as " & 1.275.ToString("G17"))
More info here
-
Oct 25th, 2011, 07:42 PM
#4
Re: Round Values
Micky_pt,
did you find which rounding method will produce the correct answer consistantly? I never knew Math.Round had that problem.
-
Oct 25th, 2011, 07:55 PM
#5
Re: Round Values
I always did this:
Format(2.65, "0.#")
And for your function why wouldn't you use ....?:
vb Code:
Public Function MyRound(ByVal valor As Decimal, ByVal casasDecimais As Integer) As Decimal
retrun Format(valor, "N" & casasDecimais.ToString))
End Function
Kris
Last edited by i00; Oct 25th, 2011 at 08:03 PM.
-
Oct 26th, 2011, 03:28 AM
#6
Thread Starter
Frenzied Member
-
Oct 26th, 2011, 10:42 AM
#7
Re: Round Values
I found it best (somewhat the same way you did) that when dealing with low-precision decimal values, like currency or anything else up to around 5 decimal places max, using the "Decimal" type alleviated most of my headaches. It's precise and exact. Traditional float types like Single and Double work best when you need to grind a lot of numeric data fast, you're not doing anything like rounding, and you don't mind the value might be a little off.
For most business and industrial apps, I use Decimal exclusively. For things like games, Singles and Doubles work fine.
-
Oct 26th, 2011, 03:11 PM
#8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|