Click here to Skip to main content
11,803,575 members (73,685 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB VB.NET
Can anyone give me a 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 2:49am
Sergey Alexandrovich Kryukov at 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.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Math.Round(number, 4 - (CType(Math.Log10(number) + 1, Integer)))
Kschuler at 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:
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 at 28-Jan-13 15:31pm
I know that.
Kschuler at 28-Jan-13 15:32pm
:) I just meant it would be nice to warn the OP.
CPallini at 28-Jan-13 15:48pm
I know that. :-)
Member 9451274 at 12-Feb-15 2:43am
This gave me a error in some combinations. - and Pyrolistical answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 CPallini 290
2 F-ES Sitecore 270
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100