Answered by:
round to nearest hundredth

How would I get the following equation to round to the nearest hundredth?
Example:
33.333333 = 33.33
50.156454 = 50.15( I don't need to do any rounding)
50 = 50 (if no decimal is needed I don't .00 after the number)
Equation:
(wins / WagerAccessDataGridView.RowCount.ToString * 100)Thursday, April 12, 2007 4:04 PM
Question
Answers

Perhaps you need Math.Round(). You can specify the number of digits and how the number is rounded.
You may want to look at Math.Ceiling() and Math.Floor(), indeed everything in the Math Class (no pun intended, if there is any...) for all your math needs.
Thursday, April 12, 2007 4:39 PMModerator 
Hi,
This looks like what you are after.>>
No rounding ( as requested ).
Regards,
S_DS
Code SnippetPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1 As Double
num1 = 50.56789
Dim myString As String
If testNumber(num1.ToString) = True Then
num1 = Int(num1 * 100) / 100
MessageBox.Show("num1 now is =" & num1.ToString)
Else
MessageBox.Show("num1= " & num1.ToString)
End If
End Sub
'Test the number as a string to see if it has a decimal point.
'If it has this Function returns TRUE.
Function testNumber(ByVal myNumber As String) As Boolean
If InStr(myNumber, ".") <> 0 Then
Return True
Else
Return False
End If
End Function
Thursday, April 12, 2007 9:22 PM 
Isn't this more appropriate? You mean you want to use fixed decimals with no rounding?
Dim vNumber As Double = 50.56789
Dim vNewNumber As Double = Int(vNumber * 100) / 100
As far as the 50 = 50 (without decimals) goes, you should handle that when you present the number to the screen (or wherever it's going). 50.00 means the same as 50, so when you go to present that value, drop the ".00" if desired.
**Note: I'm certainly not a programmer and don't claim to be one.Friday, April 13, 2007 7:17 PMModerator 
For what it is worth.
(Math.Floor(x * 100) / 100)
I seem to remember the JIT inline compiles the Floor function thus eliminating the overhead of a method call.
Whether Math.Truncate is more appropriate that Math.Floor depends on how you anticipate handling negative numbers (truncating a number always moves it towards zero)
Richard
Tuesday, April 17, 2007 2:24 PM 
I believe as a last word on this thread that something needs to be clarified....IT IS A BAD PRACTICE to manipulate numerics by converting them to strings!
Tuesday, April 17, 2007 4:18 PMModerator
All replies

Perhaps you need Math.Round(). You can specify the number of digits and how the number is rounded.
You may want to look at Math.Ceiling() and Math.Floor(), indeed everything in the Math Class (no pun intended, if there is any...) for all your math needs.
Thursday, April 12, 2007 4:39 PMModerator 
Hi,
This looks like what you are after.>>
No rounding ( as requested ).
Regards,
S_DS
Code SnippetPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1 As Double
num1 = 50.56789
Dim myString As String
If testNumber(num1.ToString) = True Then
num1 = Int(num1 * 100) / 100
MessageBox.Show("num1 now is =" & num1.ToString)
Else
MessageBox.Show("num1= " & num1.ToString)
End If
End Sub
'Test the number as a string to see if it has a decimal point.
'If it has this Function returns TRUE.
Function testNumber(ByVal myNumber As String) As Boolean
If InStr(myNumber, ".") <> 0 Then
Return True
Else
Return False
End If
End Function
Thursday, April 12, 2007 9:22 PM 
The whole point of the math class is to provide such functionality. There's absolutely no reason to perform any string manipulation with numbers. Even so, the whole 'string' comparison is redundant, anyway. That's the saddest piece of code I've seen.
Friday, April 13, 2007 11:15 AMModerator 
SJWhiteley wrote: The whole point of the math class is to provide such functionality. There's absolutely no reason to perform any string manipulation with numbers. Even so, the whole 'string' comparison is redundant, anyway. That's the saddest piece of code I've seen.
Hi,
You are entitled of course to your opinion, but the question did not require rounding and it was marked as the answer.
Why post a comment like that?
The Math CLASS does not provide every possible function at the moment, just some of the base functions, you would have to build up a complex function yourself.
Regards,
S_DS
Friday, April 13, 2007 6:18 PM 
SJWhiteley wrote: The whole point of the math class is to provide such functionality. There's absolutely no reason to perform any string manipulation with numbers. Even so, the whole 'string' comparison is redundant, anyway. That's the saddest piece of code I've seen.
Friday, April 13, 2007 6:29 PMModerator 
Isn't this more appropriate? You mean you want to use fixed decimals with no rounding?
Dim vNumber As Double = 50.56789
Dim vNewNumber As Double = Int(vNumber * 100) / 100
As far as the 50 = 50 (without decimals) goes, you should handle that when you present the number to the screen (or wherever it's going). 50.00 means the same as 50, so when you go to present that value, drop the ".00" if desired.
**Note: I'm certainly not a programmer and don't claim to be one.Friday, April 13, 2007 7:17 PMModerator 
Peter Ritchie wrote: SJWhiteley wrote: The whole point of the math class is to provide such functionality. There's absolutely no reason to perform any string manipulation with numbers. Even so, the whole 'string' comparison is redundant, anyway. That's the saddest piece of code I've seen.
In addition, double.ToString will return different results for different users depending on their locale. Checking for a decimal point in a string comparison is guaranteed to fail sometimes.
Hi Peter,
Fair comment.
I thought about the comma, then what locales use a comma instead of the decimal point?
000's separators would need to be considered too.
Regards,
S_DS
Friday, April 13, 2007 7:21 PM 
Some French locales, for one use '.' for thousand's separator and ',' for decimal separator.Friday, April 13, 2007 7:24 PMModerator

A couple of additional points  I'm suspecting that the OP is not sure what they need (a pointer to the Math class would help, I would think, in this case): the request is for rounding to the nearest hundredth, yet posts of an example which always rounds down .
The Math class has pretty much everything in it. Specifically, I'd probably use, to round to the nearest hundredth:
Math.Round(number, 2)
If the lowest number is needed, then it depends: what about negative numbers if any? If a round always to zero is required, then Math.Truncate is a better bet:
Math.Truncate(number * 100) / 100
Friday, April 13, 2007 7:46 PMModerator 
Well, this question got an interesting response...
Actually, in order to get the results precisely as the OP requested, you will probably have to revert to string manipulation. There is no one method in the Math namespace that will handle the numbers as requested. SDS was close with his answer  checking globalization for the decimal seperator would have made his suggestion valid, I believe.
Here is an example function that will truncate the decimal to the hundredth place, or entirely if it is 0. The function can also return the truncated decimal by reference if desired.
Code Snippet'Get a String from a Number by truncating the decimal at 2 places
Private Function TrimNumericString(ByVal val As Decimal, Optional ByRef result As Decimal = Nothing) As String
'Determine the decimal seperator character from the current system config
Dim ds As String = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
'Convert the number to a string
Dim vs As String = val.ToString
'Search for the decimal symbol
If vs.Contains(ds) Then
'Create variable to hold length
Dim lg As Integer
lg = vs.IndexOf(ds) + 3
'If the string is longer than 2 decimal places, truncate it
If lg < vs.Length Then
vs = vs.Substring(0, lg)
End If
End If
'Return the truncated decimal, if the user wants it
Decimal.TryParse(vs, result)
'Return the string
Return vs
End Function
Hope that helps.
Tuesday, April 17, 2007 4:54 AMModerator 
There is no need to use strings at all for such calculations, that's just absolutely crazy talk. Using strings is not a valid answer. Truncate works fine, if you want to round down, and Round works if you want to round to the nearest 100th.
What is it that makes these math functions not do what the OP wants?
Tuesday, April 17, 2007 11:44 AMModerator 
I'm joining the y=x*100\100 team tooTuesday, April 17, 2007 1:54 PM

For what it is worth.
(Math.Floor(x * 100) / 100)
I seem to remember the JIT inline compiles the Floor function thus eliminating the overhead of a method call.
Whether Math.Truncate is more appropriate that Math.Floor depends on how you anticipate handling negative numbers (truncating a number always moves it towards zero)
Richard
Tuesday, April 17, 2007 2:24 PM 
SJWhiteley wrote: There is no need to use strings at all for such calculations, that's just absolutely crazy talk. Using strings is not a valid answer. Truncate works fine, if you want to round down, and Round works if you want to round to the nearest 100th.
What is it that makes these math functions not do what the OP wants?
SJ, I'm guilty of not readying the thread fully and missing the Math.Truncate(number * 100) / 100 formula on your later post  which is the correct version of what Phil first suggested. This is, of course, the most elegant solution (and, no, I didn't think of such a simple calculation when I posted). Your original comments suggested only a single Math method, none of which will work by themselves, and that is what I had replyed to. I guess the point that I wanted to make was that while I normally agree with your posts, I did feel that you were out of line in your reply to SDS. The function I posted takes his basic idea and shows that it can be done in a solid manner that will work regardless of number format settings. Does a string manipulation take more overhead than the mathematic formula? Sure it does. Is that overhead still negligible? Absolutely (run the two functions in a loop 10 or 50 thousand times  the difference will be less than 50ms and which is faster will vary). Does the code produce the desired result consistently? Yes. I would say that makes it valid code  even if its not the best answer.
Tuesday, April 17, 2007 3:18 PMModerator 
Point taken, Reed: still friends until the next argument?
Tuesday, April 17, 2007 3:32 PMModerator 
Until then!Tuesday, April 17, 2007 3:37 PMModerator

I believe as a last word on this thread that something needs to be clarified....IT IS A BAD PRACTICE to manipulate numerics by converting them to strings!
Tuesday, April 17, 2007 4:18 PMModerator