format

  • Thread starter Thread starter Tom
  • Start date Start date
Your question should be more to the point. Usually a piece of code helps us
give you better answers.

But here it goes

dim value decimal = 131.987
label1.text = format(value, "#0")

will give you 132
 
If you are looking to just round, you can convert the number to an integer as
well.

VB.NET
Dim decimalNumber As Decimal = 131.988
Dim intNumber As Integer = decimalNumber ' intNumber will be 132
or you can show you are converting it (use one of these methods depending on
how big your integer can get)
Dim intNumber As Integer = Convert.ToInt16(doubleNumber)
Dim intNumber As Integer = Convert.ToInt32(doubleNumber)
Dim intNumber As Integer = Convert.ToInt64(doubleNumber)

or if your starting number is a double:

Dim doubleNumber As Double = 131.988
Dim intNumber As Integer = doubleNumber


C#
double doubleNumber = 131.988;
int integerNumber = Convert.ToInt16(doubleNumber);

or depending on how big your integer will get you can also use:
int integerNumber = Convert.ToInt32(doubleNumber);
int integerNumber = Convert.ToInt64(doubleNumber);

Is your starting number of 131.988 a string?
You can also use (VB.NET):
Dim doubleString As String = "131.988"
Dim intNumberFromString As Integer = doubleString
 
I have this

131.988

I want
132

how can I accomplish this?

Microsoft help documentation does not specify the precise mathematical
operation that is used when format specifiers convert decimal non-
integral numbers to integer strings. So string formatting cannot be
relied upon as means of rounding.

The System.Math class on the other have a function Round which
conforms to an IEEE standard and so can be used reliably for this
purpose.

Thus

double x = 131.988
int y = Math.Round(x)

will yield a properly rounded integer result (whatever value x happens
to be), which can then be converted to a string if desired.

This is important because there are various regimes that can be used
when the decimal lies mid way between two integers e.g. what do you do
in the case of 131.5 ? According to the IEEE standard if the lower
integer is odd then round up otherwise round down (hence 131.5 rounds
to 132 whereas 132.5 rounds to 132 rather than 133).
 
Back
Top