Rounding numbers in .NET

  • Thread starter Thread starter Kerri
  • Start date Start date
K

Kerri

Hi,

I take in a value like 17499.99 in my app.

I want to round this up to the nearest thousand

So above should be 18000. Even if I have a value like
17001 I want to round up to 18000.

Anyone know how I can do this? I have tried using the Math
class but it does not do this for me.

If i round 17499.99 using MAth.Round it gives mne 17500
buyt I want to get 18000.

Once it is anyway over 17000 I want to round it to 18000.

Thanks,
K.
 
Couldn't you use System.Math.Ceiling(Yourval/1000) * 1000? You can overload
Round with Significant Digits and decimals, but you want to round up..so I
think you may have to rig it.
HTH,

Bill
 
Kerri said:
Hi,

I take in a value like 17499.99 in my app.

I want to round this up to the nearest thousand

So above should be 18000. Even if I have a value like
17001 I want to round up to 18000.

Anyone know how I can do this? I have tried using the Math
class but it does not do this for me.

If i round 17499.99 using MAth.Round it gives mne 17500
buyt I want to get 18000.

Once it is anyway over 17000 I want to round it to 18000.

Thanks,
K.

I'm not sure how to use that class... but here's some C# code that
will do what you want.

using System;

class Round
{
public static void Main(string[] args)
{
double x = Convert.ToDouble(args[0]);
int rounded_x = ((int)x/1000) * 1000;
Console.WriteLine("x: {0}\nrounded_x: {1}", x, rounded_x);
}
}

[Compile by running:
%windir%\Microsoft.NET\Framework\v1.1.4322\csc.exe on the file
containing this text]

Sample run:

D:\Documents and Settings\nhertl>round.exe 17499.99
x: 17499.99
rounded_x: 17000

D:\Documents and Settings\nhertl>round.exe 17499
x: 17499
rounded_x: 17000
 
Back
Top