Solved

Format a number to 2 Significant Figures

Posted on 2006-07-21
2
1,012 Views
Last Modified: 2012-05-05
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?

Thanks in advance,
- Graham
0
Comment
Question by:topazg
2 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
Comment Utility
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
 
  '-- Reconsitute final answer --
  FormatSF = FormatSF * intSign
 
  '-- Answer sometimes needs padding with 0s --
  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>
<head>
</head>
<body bgcolor="FFFFFF">
  <%=FormatSF(0.000123, 2)%>
</body>
</html>


Regards,

Lee
0
 
LVL 3

Author Comment

by:topazg
Comment Utility
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"))

By adjusting:

  '-- Reconsitute final answer --
  FormatSF = FormatSF * intSign

to:

  '-- Reconsitute final answer --
  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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now