12,758,918 members (29,948 online)
Rate this:
See more:
Can anyone give me a VB.net equivalent to the below Excel Formula:

number = 1.234567
=ROUND(number, 4-(INT(LOG(number)+1)))

This example returns 1.235 to 4 sig figs.
Posted 28-Jan-13 3:49am
Sergey Alexandrovich Kryukov 28-Jan-13 12:00pm

Why do you think you need rounding? It is almost never used. Maybe you need just the string presentation with less digits? This is a different story.
—SA

Rate this:

## Solution 1

```Math.Round(number, 4 - (CType(Math.Log10(number) + 1, Integer)))
```
Kschuler 28-Jan-13 14:41pm

Be careful with Math.Round. You may need to also specify a second parm to specify how to round when a number is half way to the next integral....See here: http://msdn.microsoft.com/en-us/library/as4h66hd(v=vs.90).aspx
Which states "By default, Math.Round uses MidpointRounding.ToEven. Most people are not familiar with "rounding to even" as the alternative, "rounding away from zero" is more commonly taught in school. .NET defaults to "Rounding to even" as it is statistically superior because it doesn't share the tendency of "rounding away from zero" to round up slightly more often than it rounds down (assuming the numbers being rounded tend to be positive.) "
CPallini 28-Jan-13 15:31pm

I know that.
Kschuler 28-Jan-13 15:32pm

:) I just meant it would be nice to warn the OP.
CPallini 28-Jan-13 15:48pm

I know that. :-)
Member 9451274 12-Feb-15 2:43am

This gave me a error in some combinations.

Top Experts
Last 24hrsThis month
 Bryian Tan 190 OriginalGriff 170 ppolymorphe 159 Richard Deeming 150 Graeme_Grant 136
 OriginalGriff 4,327 Peter Leow 3,279 ppolymorphe 2,683 Karthik Bangalore 2,594 Graeme_Grant 2,387