Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you round a Double down to the nearest integer in VB .NET?

How do you round a Double down to the nearest integer in VB .NET?

like image 422
Steve Duitsman Avatar asked Dec 18 '08 15:12

Steve Duitsman


People also ask

How do I round a number in VB net?

The basic function for rounding up is Math. Ceiling(d), but the asker specifically wanted to round up after the second decimal place. This would be Math. Ceiling(d * 100) / 100.

How do you round to the nearest integer?

Rounding to the Nearest Integer If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

What is round function in VB?

Returns a number rounded to a specified number of decimal places.


2 Answers

This is pretty old, but the accepted answer of using Math.Truncate is technically incorrect: Truncate rounds towards zero, not down. For example, -1.5 is rounded to -1 instead of -2.

In order to always round down, use Math.Floor.

like image 115
Chris Pitman Avatar answered Nov 04 '22 08:11

Chris Pitman


Are we talking VB.NET or VB6? In VB.NET use Math.Truncate.

like image 38
Jon Skeet Avatar answered Nov 04 '22 10:11

Jon Skeet