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
2
Medium Priority
?
1,028 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…

636 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