Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to the next whole number (E.g. 1.0124 becomes 2) 1

Status
Not open for further replies.

salimwng

IS-IT--Management
Mar 11, 2002
134
MU
Hello Team,

I have got a special rounding to do and after many search performed in TekTips, i could not come across a solution. Can you please help me with the problem below;

After a calculation has been performed in my coding part, the result are displayed in a textbox. I DO NOT want the decimals to appear, all i need is that the result is rounded to the next whole number. The moment the value is over .0 something, it should be rounded to the next whole number. Please see example below;

Result being displayed ----> What i want to get
8.9 ------------------------------> 9
8.1 ------------------------------> 9
8.487 ----------------------------> 9
8.091 ----------------------------> 9
1.254 ----------------------------> 2
1.014 ----------------------------> 2
1.9745 ---------------------------> 2
0.0021 ---------------------------> 1

Looking ahead to hear your solutions team,

Thanks a lot for your help,
Kind regards.
Salim
 
One possible solution:

[tt]
Dim sngl As Single = 8.487
Dim s() as String
s = sngl.ToString.Split("."c)
Dim rslt As Integer = Integer.Parse(s(0))
If s.Length = 2 AndAlso Integer.Parse(s(1)) > 0 Then rslt += 1
MessageBox.Show(rslt.ToString)
[/tt]

Not tested

Hope this helps.


[vampire][bat]
 
Hi Earthandfire,

Thanks for replying. I didn't try your solution yet, but i've found something that works perfectly to the problem i was having and in case someone else have a similar problem this post can help. Here's the code below;

double d;

d = 8.0;
Console.WriteLine(Math.Ceiling(d).ToString("0"))
'Above will return result of 8

d = 8.2;
Console.WriteLine(Math.Ceiling(d).ToString("0"))
'Above will return result of 9

d = 8.9;
Console.WriteLine(Math.Ceiling(d).ToString("0"))
'Above will return result of 9


Cheers
Salim
 
What happens if the number is negative?

Try this (note TextBox4 has multi-line set to true) with positive and negative numbers in TextBox3 both with and without non zero values after the decimal point.

Code:
    Dim d As Double = Double.Parse(TextBox3.Text)
    TextBox4.Clear()
    TextBox4.AppendText("Ceiling: " + Math.Ceiling(d).ToString + Environment.NewLine)
    TextBox4.AppendText("Floor: " + Math.Floor(d).ToString + Environment.NewLine)
    Dim ints() As String = TextBox3.Text.Split("."c)
    Dim rslt As Integer = Integer.Parse(ints(0))
    Dim absrslt As Integer = Math.Abs(Integer.Parse(ints(0)))
    If ints.Length = 2 AndAlso Integer.Parse(ints(1)) > 0 Then
      rslt += 1
      absrslt += 1
    End If
    TextBox4.AppendText("Split (int): " + rslt.ToString + Environment.NewLine)
    TextBox4.AppendText("Split (abs): " + absrslt.ToString)


Hope this helps

[vampire][bat]
 
Oups..... i didn't consider negative values. But in the application i am actually working on, the results MUST always be above 0. So it shouldn't be a problem for me, but might be a problem for others!!!

You are right to consider the negative values also.

I'll try this one in the other section of my coding, where negative values WILL BE an issue.

Thanks for raising this important piece of information dude.
 
Rick, since Ceiling returns an integer, why have you included formatting?

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top