Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scientific Notation with Foo decimal places

5 views
Skip to first unread message

Patrick Gibbons

unread,
Feb 20, 2004, 2:55:30 PM2/20/04
to
It seems that Format can give me scientific notation, but only with 2
decimal points. I would like to set it to a variable amount of
decimal places.

I would like to see:
For - with Foo / See
3,300,000 - 2 / 3.30E+06 (Format(bar,"scientific") default)
3,300,000 - 1 / 3.3E+06 (Format(bar,"scientific") default)
3,300,000 - 0 / 3E+06 (Format(bar,"scientific") default)
3,300,000 - 3 / 3.300E+06 (Format(bar,"scientific") default)

So how can I do that, any ideas?

Patrick Gibbons
Witty Sig

Rick Rothstein

unread,
Feb 20, 2004, 4:30:31 PM2/20/04
to
> It seems that Format can give me scientific notation, but only with 2
> decimal points. I would like to set it to a variable amount of
> decimal places.
>
> I would like to see:
> For - with Foo / See
> 3,300,000 - 2 / 3.30E+06 (Format(bar,"scientific") default)
> 3,300,000 - 1 / 3.3E+06 (Format(bar,"scientific") default)
> 3,300,000 - 0 / 3E+06 (Format(bar,"scientific") default)
> 3,300,000 - 3 / 3.300E+06 (Format(bar,"scientific") default)

Try this function out...

Function FormatMyNumber(NumberIn As Double, _
DecimalPlaces As Long) As String
Dim Pattern As String
If DecimalPlaces > 0 Then
Pattern = "0." & String$(DecimalPlaces, "0") & "E+00"
Else
Pattern = "0E+00"
End If
FormatMyNumber = Format$(NumberIn, Pattern)
End Function

Rick - MVP


Patrick Gibbons

unread,
Feb 23, 2004, 2:55:38 PM2/23/04
to
Works well, thank you.

"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message news:<OkO4Fj$9DHA...@TK2MSFTNGP10.phx.gbl>...

Rick Rothstein

unread,
Feb 23, 2004, 3:19:25 PM2/23/04
to
You are welcome. Now that I've given you the function in an understandable
format, here's the one-liner version for all of you out there who look for
such things.<g>

Function FormatMyNumber(NumberIn As Double, _
DecimalPlaces As Long) As String

FormatMyNumber = Format$(NumberIn, "0" & _
IIf(DecimalPlaces > 0, "." & _
String(DecimalPlaces, "0"), "") & "E+00")
End Function


Rick - MVP


"Patrick Gibbons" <fnkl...@hotmail.com> wrote in message
news:22fc92c7.04022...@posting.google.com...

Patrick Gibbons

unread,
Feb 23, 2004, 4:26:48 PM2/23/04
to
I ended up with the code below, thank you again:

Function SciNotOfNumb(ByVal strOrgNumb As String, strNewNumb As String,
Optional bytDecPlaces As Byte = 0) As Long
'Purpose: Convert number to scientific notation
'Created: 2004-02-20 Rick - MVP, from
microsoft.public.vb.general.discussion
'Modifications: changed format to incorporate error checking
Dim Pattern As String

If IsNumeric(strOrgNumb) = False Then
SciNotOfNumb = 13
Exit Function
End If
If bytDecPlaces > 0 Then
Pattern = "0." & String$(bytDecPlaces, "0") & "E+00"


Else
Pattern = "0E+00"
End If

strNewNumb = Format$(CDbl(strOrgNumb), Pattern)

End Function


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Rick Rothstein

unread,
Feb 23, 2004, 5:20:21 PM2/23/04
to
From a previous post of mine...

I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And _
Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP

"Patrick Gibbons" <FnkLeRoi+HotmailCom> wrote in message
news:ugTn$Ol%23DHA...@TK2MSFTNGP11.phx.gbl...

0 new messages