?
Solved

Format a number to 2 Significant Figures

Posted on 2006-07-21
2
Medium Priority
?
1,026 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
[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
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
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
 
  '-- 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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 informatio…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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