Solved

Format a number to 2 Significant Figures

Posted on 2006-07-21
2
1,018 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
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
 
  '-- 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
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"))

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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