Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Format a number to 2 Significant Figures

Posted on 2006-07-21
Medium Priority
1,028 Views
Dear All,

I cannot see using the formatnumber function how it is possible to format a number to 2 significant figures (not decimal places), but I cannot see any other function or an easy other way to do this either.

What would be the easiest way to format a number to 2 significant figures?

- Graham
0
Question by:topazg
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 25

Accepted Solution

Lee Savidge earned 2000 total points
ID: 17152969
Hi,

I found a VB function on the web a while ago to do this. I converted it to ASP and it works just fine.

<%@ language="VBScript" %>
<%
Option Explicit

'Returns input number rounded to specified number of significant figures.
private Function FormatSF(byval dblInput, byval intSF)

Dim intCorrPower
Dim intSign

'-- Store sign of dblInput --
intSign = Sgn(dblInput)

'-- Calculate exponent of dblInput --
intCorrPower = Int(Log10(Abs(dblInput)))

FormatSF = Round(dblInput * 10 ^ ((intSF - 1) - intCorrPower))   'integer value with no sig fig
FormatSF = FormatSF * 10 ^ (intCorrPower - (intSF - 1))         'raise to original power

FormatSF = FormatSF * intSign

If InStr(FormatSF, ".") = 0 Then
If Len(FormatSF) < intSF Then FormatSF = Format(FormatSF, "##0." & String(intSF - Len(FormatSF), "0"))
End If

If intSF > 1 And Abs(FormatSF) < 1 Then
Do Until Left(Right(FormatSF, intSF), 1) <> "0" And Left(Right(FormatSF, intSF), 1) <> "."
FormatSF = FormatSF & "0"
Loop
End If
End Function

private Function Log10(x)
Log10 = Log(x) / Log(10)
End Function

%>

<html>
<body bgcolor="FFFFFF">
<%=FormatSF(0.000123, 2)%>
</body>
</html>

Regards,

Lee
0

LVL 3

Author Comment

ID: 17153143
Thanks for this ... Just as a note, it doesn't appear to work with numbers that round to exactly single digits (7 and 6.000 for example) due to not enough digits to run the following line:

If Len(FormatSF) < intSF Then FormatSF = Format(FormatSF, "##0." & String(intSF - Len(FormatSF), "0"))

FormatSF = FormatSF * intSign

to:

FormatSF = FormatSF * intSign

'-- Ensures enough decimal places after the point --
FormatSF = formatnumber(FormatSF,(intSF - 1))

This problem seems to be resolved. Great prompt answer though, cheers :)

- Graham
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month11 days, 21 hours left to enroll