Home All Groups Group Topic Archive Search About

Stupid Format$ Question

Author
24 Feb 2009 7:45 PM
Rick Raisley
I want to show a number as precisely as I can, using, say, 10 characters.
Something like:

12.3456789
1234.56789

I'm using a Double for the calculations, and have thus far done this:

'(n is a Double)

Label1.Text = Left$(CStr(n), 10)

That works okay except for two cases. First, it doesn't ROUND to those 10
digits, it truncates the text. So 0.499999999999999 reads as 0.49999999
instead of 0.50000000.

Second, at some point with small and large numbers, I'll probably want to
switch to Scientific notation, in order to have reasonable accuracy, or
display very large numbers. So a Double value of 1.23456789E-7 would show
like that, instead of 0.00000012. And numbers larger than 9999999999 could
still be displayed.

I realize I can make a series of If-Then statements, or Select Case, to
cover all the bases. But I wondered if there was a simpler way of at least
doing the first item. The second is easily covered by two statements
(greater than a given number and less than a different given number).

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

Author
24 Feb 2009 7:51 PM
Saga
Try:

round(0.499999999999999 ,10)

You might have to tweak the second parameter.
Saga
--




Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>I want to show a number as precisely as I can, using, say, 10 characters. Something like:
>
> 12.3456789
> 1234.56789
>
> I'm using a Double for the calculations, and have thus far done this:
>
> '(n is a Double)
>
> Label1.Text = Left$(CStr(n), 10)
>
> That works okay except for two cases. First, it doesn't ROUND to those 10 digits, it truncates the
> text. So 0.499999999999999 reads as 0.49999999 instead of 0.50000000.
>
> Second, at some point with small and large numbers, I'll probably want to switch to Scientific
> notation, in order to have reasonable accuracy, or display very large numbers. So a Double value
> of 1.23456789E-7 would show like that, instead of 0.00000012. And numbers larger than 9999999999
> could still be displayed.
>
> I realize I can make a series of If-Then statements, or Select Case, to cover all the bases. But I
> wondered if there was a simpler way of at least doing the first item. The second is easily covered
> by two statements (greater than a given number and less than a different given number).
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
>
Author
24 Feb 2009 7:54 PM
dpb
Rick Raisley wrote:
> I want to show a number as precisely as I can, using, say, 10 characters.
> Something like:
....
> Second, at some point with small and large numbers, I'll probably want to
> switch to Scientific notation, in order to have reasonable accuracy, or
> display very large numbers. ...
> ...I wondered if there was a simpler way ...

Sure, use Fortran G format... <vbg>

(While _mostly_ "just joshin'" I have in the past done similar
manipulations by using Fortran in a DLL and returning the string to VB
for printing because it is so much simpler to let the Fortran runtime
take care of it via internal writes.  I suppose it could be done w/ the
C runtime as well if one is more familiar w/ C than Fortran but I'm not
conversant enough w/ the C formats to know the characteristic behavior
of g in C.)

--
Author
24 Feb 2009 8:25 PM
Richard Mueller [MVP]
A solution that does not handle scientific notation:

    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>I want to show a number as precisely as I can, using, say, 10 characters.
>Something like:
>
> 12.3456789
> 1234.56789
>
> I'm using a Double for the calculations, and have thus far done this:
>
> '(n is a Double)
>
> Label1.Text = Left$(CStr(n), 10)
>
> That works okay except for two cases. First, it doesn't ROUND to those 10
> digits, it truncates the text. So 0.499999999999999 reads as 0.49999999
> instead of 0.50000000.
>
> Second, at some point with small and large numbers, I'll probably want to
> switch to Scientific notation, in order to have reasonable accuracy, or
> display very large numbers. So a Double value of 1.23456789E-7 would show
> like that, instead of 0.00000012. And numbers larger than 9999999999 could
> still be displayed.
>
> I realize I can make a series of If-Then statements, or Select Case, to
> cover all the bases. But I wondered if there was a simpler way of at least
> doing the first item. The second is easily covered by two statements
> (greater than a given number and less than a different given number).
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
>
Author
25 Feb 2009 12:59 PM
Rick Raisley
It also doesn't work well with negative numbers, but thanks.

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

Show quoteHide quote
"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>A solution that does not handle scientific notation:
>
>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>I want to show a number as precisely as I can, using, say, 10 characters.
>>Something like:
>>
>> 12.3456789
>> 1234.56789
>>
>> I'm using a Double for the calculations, and have thus far done this:
>>
>> '(n is a Double)
>>
>> Label1.Text = Left$(CStr(n), 10)
>>
>> That works okay except for two cases. First, it doesn't ROUND to those 10
>> digits, it truncates the text. So 0.499999999999999 reads as 0.49999999
>> instead of 0.50000000.
>>
>> Second, at some point with small and large numbers, I'll probably want to
>> switch to Scientific notation, in order to have reasonable accuracy, or
>> display very large numbers. So a Double value of 1.23456789E-7 would show
>> like that, instead of 0.00000012. And numbers larger than 9999999999
>> could still be displayed.
>>
>> I realize I can make a series of If-Then statements, or Select Case, to
>> cover all the bases. But I wondered if there was a simpler way of at
>> least doing the first item. The second is easily covered by two
>> statements (greater than a given number and less than a different given
>> number).
>>
>> --
>> Regards,
>>
>> Rick Raisley
>> heavymetal-A-T-bellsouth-D-O-T-net
>>
>>
>
>
Author
25 Feb 2009 1:26 PM
Rick Raisley
I'm sure this is more complex than necessary, but I've written the following
routine to handle my requirement:

Function RoundLen(ByVal a As Double, ByVal length As Integer) As String
   Dim g As String, x As Integer, ohs As String, neg As String

   If a = 0 Then
      'Don't pad zeros. Makes them look more obvious.
      RoundLen = "0"
      Exit Function
   End If
   If a < 0 Then neg = "-"
   'I prefer constant length numbers with negative sign added. If total
length must be constant, use length = length - 1
   a = Abs(a)
   ohs = String(length, "0")
   x = Int(Log(a) / Log(10#))
   If a > CDbl(String(length, "9")) Then
      'For very large numbers, revert to scientific notation
      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
   ElseIf x / length < -0.5 Then
      'For very small numbers, revert to scientific notation. x/length seems
to optimize when to use it for most displayed precision
      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
   ElseIf a < 1 Then
      'Handles numbers less than 1
      g = Format$(a, "0." & Left$(ohs, length - 2))
   ElseIf x > length - 2 Then
      'Handles numbers of maximum length only, with decimal
      g = Format$(a, Left$(ohs, length))
   Else
      'Handles "normal" numbers, with 1 or more places on each side of
decimal.
      g = Format$(a, "0." & Left$(ohs, length - x - 2))
   End If
   RoundLen = neg & g
End Function

Any simplification suggestions would be appreciated. ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
> It also doesn't work well with negative numbers, but thanks.
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
> message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>A solution that does not handle scientific notation:
>>
>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>
>> --
>> Richard Mueller
>> MVP Directory Services
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>I want to show a number as precisely as I can, using, say, 10 characters.
>>>Something like:
>>>
>>> 12.3456789
>>> 1234.56789
>>>
>>> I'm using a Double for the calculations, and have thus far done this:
>>>
>>> '(n is a Double)
>>>
>>> Label1.Text = Left$(CStr(n), 10)
>>>
>>> That works okay except for two cases. First, it doesn't ROUND to those
>>> 10 digits, it truncates the text. So 0.499999999999999 reads as
>>> 0.49999999 instead of 0.50000000.
>>>
>>> Second, at some point with small and large numbers, I'll probably want
>>> to switch to Scientific notation, in order to have reasonable accuracy,
>>> or display very large numbers. So a Double value of 1.23456789E-7 would
>>> show like that, instead of 0.00000012. And numbers larger than
>>> 9999999999 could still be displayed.
>>>
>>> I realize I can make a series of If-Then statements, or Select Case, to
>>> cover all the bases. But I wondered if there was a simpler way of at
>>> least doing the first item. The second is easily covered by two
>>> statements (greater than a given number and less than a different given
>>> number).
>>>
>>> --
>>> Regards,
>>>
>>> Rick Raisley
>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>
>>>
>>
>>
>
>
Author
25 Feb 2009 3:36 PM
Rick Rothstein
Sorry about my other post... I misread what you wanted to do. Give the
following function a try (it is a little shorter and does less work than
your function)...

Function RoundLen(Value As Double, Length As Integer) As Double
  Dim Num As Double
  Dim Parts() As String
  Num = CDbl(Format(Value, "0.##############################;;0"))
  If InStr(Num, "E") Then
    Parts = Split(CStr(Num), "E")
    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" & Parts(1))
  ElseIf Num <> 0 Then
    RoundLen = Format(Num, "0." & String(Length, "0"))
  End If
End Function

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
> I'm sure this is more complex than necessary, but I've written the
> following routine to handle my requirement:
>
> Function RoundLen(ByVal a As Double, ByVal length As Integer) As String
>   Dim g As String, x As Integer, ohs As String, neg As String
>
>   If a = 0 Then
>      'Don't pad zeros. Makes them look more obvious.
>      RoundLen = "0"
>      Exit Function
>   End If
>   If a < 0 Then neg = "-"
>   'I prefer constant length numbers with negative sign added. If total
> length must be constant, use length = length - 1
>   a = Abs(a)
>   ohs = String(length, "0")
>   x = Int(Log(a) / Log(10#))
>   If a > CDbl(String(length, "9")) Then
>      'For very large numbers, revert to scientific notation
>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>   ElseIf x / length < -0.5 Then
>      'For very small numbers, revert to scientific notation. x/length
> seems to optimize when to use it for most displayed precision
>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>   ElseIf a < 1 Then
>      'Handles numbers less than 1
>      g = Format$(a, "0." & Left$(ohs, length - 2))
>   ElseIf x > length - 2 Then
>      'Handles numbers of maximum length only, with decimal
>      g = Format$(a, Left$(ohs, length))
>   Else
>      'Handles "normal" numbers, with 1 or more places on each side of
> decimal.
>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>   End If
>   RoundLen = neg & g
> End Function
>
> Any simplification suggestions would be appreciated. ;-)
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>> It also doesn't work well with negative numbers, but thanks.
>>
>> --
>> Regards,
>>
>> Rick Raisley
>> heavymetal-A-T-bellsouth-D-O-T-net
>>
>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
>> message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>A solution that does not handle scientific notation:
>>>
>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>
>>> --
>>> Richard Mueller
>>> MVP Directory Services
>>> Hilltop Lab - http://www.rlmueller.net
>>> --
>>>
>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>I want to show a number as precisely as I can, using, say, 10
>>>>characters. Something like:
>>>>
>>>> 12.3456789
>>>> 1234.56789
>>>>
>>>> I'm using a Double for the calculations, and have thus far done this:
>>>>
>>>> '(n is a Double)
>>>>
>>>> Label1.Text = Left$(CStr(n), 10)
>>>>
>>>> That works okay except for two cases. First, it doesn't ROUND to those
>>>> 10 digits, it truncates the text. So 0.499999999999999 reads as
>>>> 0.49999999 instead of 0.50000000.
>>>>
>>>> Second, at some point with small and large numbers, I'll probably want
>>>> to switch to Scientific notation, in order to have reasonable accuracy,
>>>> or display very large numbers. So a Double value of 1.23456789E-7 would
>>>> show like that, instead of 0.00000012. And numbers larger than
>>>> 9999999999 could still be displayed.
>>>>
>>>> I realize I can make a series of If-Then statements, or Select Case, to
>>>> cover all the bases. But I wondered if there was a simpler way of at
>>>> least doing the first item. The second is easily covered by two
>>>> statements (greater than a given number and less than a different given
>>>> number).
>>>>
>>>> --
>>>> Regards,
>>>>
>>>> Rick Raisley
>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
25 Feb 2009 4:53 PM
Rick Rothstein
Slight modification to return a String instead of a Double and to suppress
the decimal point from values rounded to Length = 0...

Function RoundLen(Value As Double, Length As Integer) As String
  Dim Num As Double
  Dim Parts() As String
  Num = CDbl(Format(Value, "0.##############################;;0"))
  If InStr(Num, "E") Then
    Parts = Split(CStr(Num), "E")
    RoundLen = Format(Parts(0), "0" & Left(".", -(Length <> 0)) & _
               String(Length, "0") & "E" & Parts(1))
  ElseIf Num <> 0 Then
    RoundLen = Format(Num, "0" & Left(".", _
               -(Length <> 0)) & String(Length, "0"))
  Else
    RoundLen = "0"
  End If
End Function

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:eZlwp71lJHA.4520@TK2MSFTNGP03.phx.gbl...
> Sorry about my other post... I misread what you wanted to do. Give the
> following function a try (it is a little shorter and does less work than
> your function)...
>
> Function RoundLen(Value As Double, Length As Integer) As Double
>  Dim Num As Double
>  Dim Parts() As String
>  Num = CDbl(Format(Value, "0.##############################;;0"))
>  If InStr(Num, "E") Then
>    Parts = Split(CStr(Num), "E")
>    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" &
> Parts(1))
>  ElseIf Num <> 0 Then
>    RoundLen = Format(Num, "0." & String(Length, "0"))
>  End If
> End Function
>
> --
> Rick (MVP - Excel)
>
>
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
>> I'm sure this is more complex than necessary, but I've written the
>> following routine to handle my requirement:
>>
>> Function RoundLen(ByVal a As Double, ByVal length As Integer) As String
>>   Dim g As String, x As Integer, ohs As String, neg As String
>>
>>   If a = 0 Then
>>      'Don't pad zeros. Makes them look more obvious.
>>      RoundLen = "0"
>>      Exit Function
>>   End If
>>   If a < 0 Then neg = "-"
>>   'I prefer constant length numbers with negative sign added. If total
>> length must be constant, use length = length - 1
>>   a = Abs(a)
>>   ohs = String(length, "0")
>>   x = Int(Log(a) / Log(10#))
>>   If a > CDbl(String(length, "9")) Then
>>      'For very large numbers, revert to scientific notation
>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>   ElseIf x / length < -0.5 Then
>>      'For very small numbers, revert to scientific notation. x/length
>> seems to optimize when to use it for most displayed precision
>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>   ElseIf a < 1 Then
>>      'Handles numbers less than 1
>>      g = Format$(a, "0." & Left$(ohs, length - 2))
>>   ElseIf x > length - 2 Then
>>      'Handles numbers of maximum length only, with decimal
>>      g = Format$(a, Left$(ohs, length))
>>   Else
>>      'Handles "normal" numbers, with 1 or more places on each side of
>> decimal.
>>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>>   End If
>>   RoundLen = neg & g
>> End Function
>>
>> Any simplification suggestions would be appreciated. ;-)
>>
>> --
>> Regards,
>>
>> Rick Raisley
>> heavymetal-A-T-bellsouth-D-O-T-net
>>
>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>>> It also doesn't work well with negative numbers, but thanks.
>>>
>>> --
>>> Regards,
>>>
>>> Rick Raisley
>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>
>>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
>>> message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>>A solution that does not handle scientific notation:
>>>>
>>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>>
>>>> --
>>>> Richard Mueller
>>>> MVP Directory Services
>>>> Hilltop Lab - http://www.rlmueller.net
>>>> --
>>>>
>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>>I want to show a number as precisely as I can, using, say, 10
>>>>>characters. Something like:
>>>>>
>>>>> 12.3456789
>>>>> 1234.56789
>>>>>
>>>>> I'm using a Double for the calculations, and have thus far done this:
>>>>>
>>>>> '(n is a Double)
>>>>>
>>>>> Label1.Text = Left$(CStr(n), 10)
>>>>>
>>>>> That works okay except for two cases. First, it doesn't ROUND to those
>>>>> 10 digits, it truncates the text. So 0.499999999999999 reads as
>>>>> 0.49999999 instead of 0.50000000.
>>>>>
>>>>> Second, at some point with small and large numbers, I'll probably want
>>>>> to switch to Scientific notation, in order to have reasonable
>>>>> accuracy, or display very large numbers. So a Double value of
>>>>> 1.23456789E-7 would show like that, instead of 0.00000012. And numbers
>>>>> larger than 9999999999 could still be displayed.
>>>>>
>>>>> I realize I can make a series of If-Then statements, or Select Case,
>>>>> to cover all the bases. But I wondered if there was a simpler way of
>>>>> at least doing the first item. The second is easily covered by two
>>>>> statements (greater than a given number and less than a different
>>>>> given number).
>>>>>
>>>>> --
>>>>> Regards,
>>>>>
>>>>> Rick Raisley
>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
Author
25 Feb 2009 7:26 PM
christery
> >>>>> --
> >>>>> Regards,
>
> >>>>> Rick Raisley
> >>>>> heavymetal-A-T-bellsouth-D-O-T-net- Dölj citerad text -
>
> - Visa citerad text -

Nice Rick, and now a one liner ;)
just tried something but didnt go with the E notations...
stole the idea from all above (well not the E notations), just wanted
to see if it works.. and in XL (VBA), as I have no VB6 at home :(
//CY


Public Sub test()
Debug.Print x(0.5)
Debug.Print x(0.49999)
Debug.Print x(123456.49999)

End Sub


Private Function x(y As Double) As String
x = Left(Format(y, "0.##########") & "0000000000", 10)
End Function
Author
25 Feb 2009 8:12 PM
Rick Rothstein
Just to clarify, the Length argument in my function sets the number of
decimal places to round to, NOT the total number of significant digits to be
returned independent of the decimal point's location.

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:%23Rvo4m2lJHA.3760@TK2MSFTNGP03.phx.gbl...
> Slight modification to return a String instead of a Double and to suppress
> the decimal point from values rounded to Length = 0...
>
> Function RoundLen(Value As Double, Length As Integer) As String
>  Dim Num As Double
>  Dim Parts() As String
>  Num = CDbl(Format(Value, "0.##############################;;0"))
>  If InStr(Num, "E") Then
>    Parts = Split(CStr(Num), "E")
>    RoundLen = Format(Parts(0), "0" & Left(".", -(Length <> 0)) & _
>               String(Length, "0") & "E" & Parts(1))
>  ElseIf Num <> 0 Then
>    RoundLen = Format(Num, "0" & Left(".", _
>               -(Length <> 0)) & String(Length, "0"))
>  Else
>    RoundLen = "0"
>  End If
> End Function
>
> --
> Rick (MVP - Excel)
>
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
> news:eZlwp71lJHA.4520@TK2MSFTNGP03.phx.gbl...
>> Sorry about my other post... I misread what you wanted to do. Give the
>> following function a try (it is a little shorter and does less work than
>> your function)...
>>
>> Function RoundLen(Value As Double, Length As Integer) As Double
>>  Dim Num As Double
>>  Dim Parts() As String
>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>  If InStr(Num, "E") Then
>>    Parts = Split(CStr(Num), "E")
>>    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" &
>> Parts(1))
>>  ElseIf Num <> 0 Then
>>    RoundLen = Format(Num, "0." & String(Length, "0"))
>>  End If
>> End Function
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>> news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
>>> I'm sure this is more complex than necessary, but I've written the
>>> following routine to handle my requirement:
>>>
>>> Function RoundLen(ByVal a As Double, ByVal length As Integer) As String
>>>   Dim g As String, x As Integer, ohs As String, neg As String
>>>
>>>   If a = 0 Then
>>>      'Don't pad zeros. Makes them look more obvious.
>>>      RoundLen = "0"
>>>      Exit Function
>>>   End If
>>>   If a < 0 Then neg = "-"
>>>   'I prefer constant length numbers with negative sign added. If total
>>> length must be constant, use length = length - 1
>>>   a = Abs(a)
>>>   ohs = String(length, "0")
>>>   x = Int(Log(a) / Log(10#))
>>>   If a > CDbl(String(length, "9")) Then
>>>      'For very large numbers, revert to scientific notation
>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>   ElseIf x / length < -0.5 Then
>>>      'For very small numbers, revert to scientific notation. x/length
>>> seems to optimize when to use it for most displayed precision
>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>   ElseIf a < 1 Then
>>>      'Handles numbers less than 1
>>>      g = Format$(a, "0." & Left$(ohs, length - 2))
>>>   ElseIf x > length - 2 Then
>>>      'Handles numbers of maximum length only, with decimal
>>>      g = Format$(a, Left$(ohs, length))
>>>   Else
>>>      'Handles "normal" numbers, with 1 or more places on each side of
>>> decimal.
>>>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>>>   End If
>>>   RoundLen = neg & g
>>> End Function
>>>
>>> Any simplification suggestions would be appreciated. ;-)
>>>
>>> --
>>> Regards,
>>>
>>> Rick Raisley
>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>
>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>>>> It also doesn't work well with negative numbers, but thanks.
>>>>
>>>> --
>>>> Regards,
>>>>
>>>> Rick Raisley
>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>
>>>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote
>>>> in message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>>>A solution that does not handle scientific notation:
>>>>>
>>>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>>>
>>>>> --
>>>>> Richard Mueller
>>>>> MVP Directory Services
>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>> --
>>>>>
>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>>>I want to show a number as precisely as I can, using, say, 10
>>>>>>characters. Something like:
>>>>>>
>>>>>> 12.3456789
>>>>>> 1234.56789
>>>>>>
>>>>>> I'm using a Double for the calculations, and have thus far done this:
>>>>>>
>>>>>> '(n is a Double)
>>>>>>
>>>>>> Label1.Text = Left$(CStr(n), 10)
>>>>>>
>>>>>> That works okay except for two cases. First, it doesn't ROUND to
>>>>>> those 10 digits, it truncates the text. So 0.499999999999999 reads as
>>>>>> 0.49999999 instead of 0.50000000.
>>>>>>
>>>>>> Second, at some point with small and large numbers, I'll probably
>>>>>> want to switch to Scientific notation, in order to have reasonable
>>>>>> accuracy, or display very large numbers. So a Double value of
>>>>>> 1.23456789E-7 would show like that, instead of 0.00000012. And
>>>>>> numbers larger than 9999999999 could still be displayed.
>>>>>>
>>>>>> I realize I can make a series of If-Then statements, or Select Case,
>>>>>> to cover all the bases. But I wondered if there was a simpler way of
>>>>>> at least doing the first item. The second is easily covered by two
>>>>>> statements (greater than a given number and less than a different
>>>>>> given number).
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>>
>>>>>> Rick Raisley
>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
Author
26 Feb 2009 1:08 PM
Rick Raisley
Yeah, I noticed. I want the total string length to always be a set maximum
value, except I've decided that it looks best (and there's room) for the
negative sign to be in addition to the number of desired places. My code
does all that, but it's messy (hidden in a function, though, at least). ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:Obcd1V4lJHA.2384@TK2MSFTNGP04.phx.gbl...
> Just to clarify, the Length argument in my function sets the number of
> decimal places to round to, NOT the total number of significant digits to
> be returned independent of the decimal point's location.
>
> --
> Rick (MVP - Excel)
>
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
> news:%23Rvo4m2lJHA.3760@TK2MSFTNGP03.phx.gbl...
>> Slight modification to return a String instead of a Double and to
>> suppress the decimal point from values rounded to Length = 0...
>>
>> Function RoundLen(Value As Double, Length As Integer) As String
>>  Dim Num As Double
>>  Dim Parts() As String
>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>  If InStr(Num, "E") Then
>>    Parts = Split(CStr(Num), "E")
>>    RoundLen = Format(Parts(0), "0" & Left(".", -(Length <> 0)) & _
>>               String(Length, "0") & "E" & Parts(1))
>>  ElseIf Num <> 0 Then
>>    RoundLen = Format(Num, "0" & Left(".", _
>>               -(Length <> 0)) & String(Length, "0"))
>>  Else
>>    RoundLen = "0"
>>  End If
>> End Function
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>> news:eZlwp71lJHA.4520@TK2MSFTNGP03.phx.gbl...
>>> Sorry about my other post... I misread what you wanted to do. Give the
>>> following function a try (it is a little shorter and does less work than
>>> your function)...
>>>
>>> Function RoundLen(Value As Double, Length As Integer) As Double
>>>  Dim Num As Double
>>>  Dim Parts() As String
>>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>>  If InStr(Num, "E") Then
>>>    Parts = Split(CStr(Num), "E")
>>>    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" &
>>> Parts(1))
>>>  ElseIf Num <> 0 Then
>>>    RoundLen = Format(Num, "0." & String(Length, "0"))
>>>  End If
>>> End Function
>>>
>>> --
>>> Rick (MVP - Excel)
>>>
>>>
>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>> news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
>>>> I'm sure this is more complex than necessary, but I've written the
>>>> following routine to handle my requirement:
>>>>
>>>> Function RoundLen(ByVal a As Double, ByVal length As Integer) As String
>>>>   Dim g As String, x As Integer, ohs As String, neg As String
>>>>
>>>>   If a = 0 Then
>>>>      'Don't pad zeros. Makes them look more obvious.
>>>>      RoundLen = "0"
>>>>      Exit Function
>>>>   End If
>>>>   If a < 0 Then neg = "-"
>>>>   'I prefer constant length numbers with negative sign added. If total
>>>> length must be constant, use length = length - 1
>>>>   a = Abs(a)
>>>>   ohs = String(length, "0")
>>>>   x = Int(Log(a) / Log(10#))
>>>>   If a > CDbl(String(length, "9")) Then
>>>>      'For very large numbers, revert to scientific notation
>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>   ElseIf x / length < -0.5 Then
>>>>      'For very small numbers, revert to scientific notation. x/length
>>>> seems to optimize when to use it for most displayed precision
>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>   ElseIf a < 1 Then
>>>>      'Handles numbers less than 1
>>>>      g = Format$(a, "0." & Left$(ohs, length - 2))
>>>>   ElseIf x > length - 2 Then
>>>>      'Handles numbers of maximum length only, with decimal
>>>>      g = Format$(a, Left$(ohs, length))
>>>>   Else
>>>>      'Handles "normal" numbers, with 1 or more places on each side of
>>>> decimal.
>>>>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>>>>   End If
>>>>   RoundLen = neg & g
>>>> End Function
>>>>
>>>> Any simplification suggestions would be appreciated. ;-)
>>>>
>>>> --
>>>> Regards,
>>>>
>>>> Rick Raisley
>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>
>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>>>>> It also doesn't work well with negative numbers, but thanks.
>>>>>
>>>>> --
>>>>> Regards,
>>>>>
>>>>> Rick Raisley
>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>
>>>>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote
>>>>> in message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>>>>A solution that does not handle scientific notation:
>>>>>>
>>>>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>>>>
>>>>>> --
>>>>>> Richard Mueller
>>>>>> MVP Directory Services
>>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>>> --
>>>>>>
>>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>>>>I want to show a number as precisely as I can, using, say, 10
>>>>>>>characters. Something like:
>>>>>>>
>>>>>>> 12.3456789
>>>>>>> 1234.56789
>>>>>>>
>>>>>>> I'm using a Double for the calculations, and have thus far done
>>>>>>> this:
>>>>>>>
>>>>>>> '(n is a Double)
>>>>>>>
>>>>>>> Label1.Text = Left$(CStr(n), 10)
>>>>>>>
>>>>>>> That works okay except for two cases. First, it doesn't ROUND to
>>>>>>> those 10 digits, it truncates the text. So 0.499999999999999 reads
>>>>>>> as 0.49999999 instead of 0.50000000.
>>>>>>>
>>>>>>> Second, at some point with small and large numbers, I'll probably
>>>>>>> want to switch to Scientific notation, in order to have reasonable
>>>>>>> accuracy, or display very large numbers. So a Double value of
>>>>>>> 1.23456789E-7 would show like that, instead of 0.00000012. And
>>>>>>> numbers larger than 9999999999 could still be displayed.
>>>>>>>
>>>>>>> I realize I can make a series of If-Then statements, or Select Case,
>>>>>>> to cover all the bases. But I wondered if there was a simpler way of
>>>>>>> at least doing the first item. The second is easily covered by two
>>>>>>> statements (greater than a given number and less than a different
>>>>>>> given number).
>>>>>>>
>>>>>>> --
>>>>>>> Regards,
>>>>>>>
>>>>>>> Rick Raisley
>>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
Author
26 Feb 2009 2:26 PM
Rick Rothstein
I guess I am not sure of your requirements, but I got your function to fail
with these two test values...

Debug.Print RoundLen(12345, 4)
Debug.Print RoundLen(123456.49999, 4)

Is there some required length for you values? How did you want to handle
values with E-notation... is the E## (where each # is a digit) part of the
desired length or is the numeric part to be of the specified length and the
E## concatenated onto it afterwards?

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:u8GkLNBmJHA.4760@TK2MSFTNGP04.phx.gbl...
> Yeah, I noticed. I want the total string length to always be a set maximum
> value, except I've decided that it looks best (and there's room) for the
> negative sign to be in addition to the number of desired places. My code
> does all that, but it's messy (hidden in a function, though, at least).
> ;-)
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
> news:Obcd1V4lJHA.2384@TK2MSFTNGP04.phx.gbl...
>> Just to clarify, the Length argument in my function sets the number of
>> decimal places to round to, NOT the total number of significant digits to
>> be returned independent of the decimal point's location.
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>> news:%23Rvo4m2lJHA.3760@TK2MSFTNGP03.phx.gbl...
>>> Slight modification to return a String instead of a Double and to
>>> suppress the decimal point from values rounded to Length = 0...
>>>
>>> Function RoundLen(Value As Double, Length As Integer) As String
>>>  Dim Num As Double
>>>  Dim Parts() As String
>>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>>  If InStr(Num, "E") Then
>>>    Parts = Split(CStr(Num), "E")
>>>    RoundLen = Format(Parts(0), "0" & Left(".", -(Length <> 0)) & _
>>>               String(Length, "0") & "E" & Parts(1))
>>>  ElseIf Num <> 0 Then
>>>    RoundLen = Format(Num, "0" & Left(".", _
>>>               -(Length <> 0)) & String(Length, "0"))
>>>  Else
>>>    RoundLen = "0"
>>>  End If
>>> End Function
>>>
>>> --
>>> Rick (MVP - Excel)
>>>
>>>
>>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>>> news:eZlwp71lJHA.4520@TK2MSFTNGP03.phx.gbl...
>>>> Sorry about my other post... I misread what you wanted to do. Give the
>>>> following function a try (it is a little shorter and does less work
>>>> than your function)...
>>>>
>>>> Function RoundLen(Value As Double, Length As Integer) As Double
>>>>  Dim Num As Double
>>>>  Dim Parts() As String
>>>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>>>  If InStr(Num, "E") Then
>>>>    Parts = Split(CStr(Num), "E")
>>>>    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" &
>>>> Parts(1))
>>>>  ElseIf Num <> 0 Then
>>>>    RoundLen = Format(Num, "0." & String(Length, "0"))
>>>>  End If
>>>> End Function
>>>>
>>>> --
>>>> Rick (MVP - Excel)
>>>>
>>>>
>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>> news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
>>>>> I'm sure this is more complex than necessary, but I've written the
>>>>> following routine to handle my requirement:
>>>>>
>>>>> Function RoundLen(ByVal a As Double, ByVal length As Integer) As
>>>>> String
>>>>>   Dim g As String, x As Integer, ohs As String, neg As String
>>>>>
>>>>>   If a = 0 Then
>>>>>      'Don't pad zeros. Makes them look more obvious.
>>>>>      RoundLen = "0"
>>>>>      Exit Function
>>>>>   End If
>>>>>   If a < 0 Then neg = "-"
>>>>>   'I prefer constant length numbers with negative sign added. If total
>>>>> length must be constant, use length = length - 1
>>>>>   a = Abs(a)
>>>>>   ohs = String(length, "0")
>>>>>   x = Int(Log(a) / Log(10#))
>>>>>   If a > CDbl(String(length, "9")) Then
>>>>>      'For very large numbers, revert to scientific notation
>>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>>   ElseIf x / length < -0.5 Then
>>>>>      'For very small numbers, revert to scientific notation. x/length
>>>>> seems to optimize when to use it for most displayed precision
>>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>>   ElseIf a < 1 Then
>>>>>      'Handles numbers less than 1
>>>>>      g = Format$(a, "0." & Left$(ohs, length - 2))
>>>>>   ElseIf x > length - 2 Then
>>>>>      'Handles numbers of maximum length only, with decimal
>>>>>      g = Format$(a, Left$(ohs, length))
>>>>>   Else
>>>>>      'Handles "normal" numbers, with 1 or more places on each side of
>>>>> decimal.
>>>>>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>>>>>   End If
>>>>>   RoundLen = neg & g
>>>>> End Function
>>>>>
>>>>> Any simplification suggestions would be appreciated. ;-)
>>>>>
>>>>> --
>>>>> Regards,
>>>>>
>>>>> Rick Raisley
>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>
>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>>>>>> It also doesn't work well with negative numbers, but thanks.
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>>
>>>>>> Rick Raisley
>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>
>>>>>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote
>>>>>> in message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>>>>>A solution that does not handle scientific notation:
>>>>>>>
>>>>>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>>>>>
>>>>>>> --
>>>>>>> Richard Mueller
>>>>>>> MVP Directory Services
>>>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>>>> --
>>>>>>>
>>>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>>>>>I want to show a number as precisely as I can, using, say, 10
>>>>>>>>characters. Something like:
>>>>>>>>
>>>>>>>> 12.3456789
>>>>>>>> 1234.56789
>>>>>>>>
>>>>>>>> I'm using a Double for the calculations, and have thus far done
>>>>>>>> this:
>>>>>>>>
>>>>>>>> '(n is a Double)
>>>>>>>>
>>>>>>>> Label1.Text = Left$(CStr(n), 10)
>>>>>>>>
>>>>>>>> That works okay except for two cases. First, it doesn't ROUND to
>>>>>>>> those 10 digits, it truncates the text. So 0.499999999999999 reads
>>>>>>>> as 0.49999999 instead of 0.50000000.
>>>>>>>>
>>>>>>>> Second, at some point with small and large numbers, I'll probably
>>>>>>>> want to switch to Scientific notation, in order to have reasonable
>>>>>>>> accuracy, or display very large numbers. So a Double value of
>>>>>>>> 1.23456789E-7 would show like that, instead of 0.00000012. And
>>>>>>>> numbers larger than 9999999999 could still be displayed.
>>>>>>>>
>>>>>>>> I realize I can make a series of If-Then statements, or Select
>>>>>>>> Case, to cover all the bases. But I wondered if there was a simpler
>>>>>>>> way of at least doing the first item. The second is easily covered
>>>>>>>> by two statements (greater than a given number and less than a
>>>>>>>> different given number).
>>>>>>>>
>>>>>>>> --
>>>>>>>> Regards,
>>>>>>>>
>>>>>>>> Rick Raisley
>>>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
>
Author
27 Feb 2009 1:23 PM
Rick Raisley
Yeah, my function wouldn't work for only 4 length. For my purposes, I'd
never have anything less than 8. For consistency in notation, when using
Scientific notation, the format would be:

1.2 E-06

That in itself is 8 characters long, and is the very shortest I'd consider
for this function (for my purposes - not a general Format function, of
course).

As to my requirements, they are simply to display the maximum number of
significant digits possible, using all of the available places. For 9
digits, for example, examples of this would be:

1.234 E+10
1.234 E+09
1.234 E+08
123456789
12345678. (don't like this one, but I prefer them all the same length)
1234567.8
123456.78
12345.678
1234.5678
123.45678
12.345678
1.2345678
0.1234567
0.0123456
0.0012345
0.0001234
1.234 E-05
1.234 E-06
etc.

I believe my function, as convoluted as it is, handles this.

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:e$Chx4BmJHA.5652@TK2MSFTNGP02.phx.gbl...
>I guess I am not sure of your requirements, but I got your function to fail
>with these two test values...
>
> Debug.Print RoundLen(12345, 4)
> Debug.Print RoundLen(123456.49999, 4)
>
> Is there some required length for you values? How did you want to handle
> values with E-notation... is the E## (where each # is a digit) part of the
> desired length or is the numeric part to be of the specified length and
> the E## concatenated onto it afterwards?
>
> --
> Rick (MVP - Excel)
>
>
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:u8GkLNBmJHA.4760@TK2MSFTNGP04.phx.gbl...
>> Yeah, I noticed. I want the total string length to always be a set
>> maximum value, except I've decided that it looks best (and there's room)
>> for the negative sign to be in addition to the number of desired places.
>> My code does all that, but it's messy (hidden in a function, though, at
>> least). ;-)
>>
>> --
>> Regards,
>>
>> Rick Raisley
>> heavymetal-A-T-bellsouth-D-O-T-net
>>
>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>> news:Obcd1V4lJHA.2384@TK2MSFTNGP04.phx.gbl...
>>> Just to clarify, the Length argument in my function sets the number of
>>> decimal places to round to, NOT the total number of significant digits
>>> to be returned independent of the decimal point's location.
>>>
>>> --
>>> Rick (MVP - Excel)
>>>
>>>
>>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>>> news:%23Rvo4m2lJHA.3760@TK2MSFTNGP03.phx.gbl...
>>>> Slight modification to return a String instead of a Double and to
>>>> suppress the decimal point from values rounded to Length = 0...
>>>>
>>>> Function RoundLen(Value As Double, Length As Integer) As String
>>>>  Dim Num As Double
>>>>  Dim Parts() As String
>>>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>>>  If InStr(Num, "E") Then
>>>>    Parts = Split(CStr(Num), "E")
>>>>    RoundLen = Format(Parts(0), "0" & Left(".", -(Length <> 0)) & _
>>>>               String(Length, "0") & "E" & Parts(1))
>>>>  ElseIf Num <> 0 Then
>>>>    RoundLen = Format(Num, "0" & Left(".", _
>>>>               -(Length <> 0)) & String(Length, "0"))
>>>>  Else
>>>>    RoundLen = "0"
>>>>  End If
>>>> End Function
>>>>
>>>> --
>>>> Rick (MVP - Excel)
>>>>
>>>>
>>>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>>>> news:eZlwp71lJHA.4520@TK2MSFTNGP03.phx.gbl...
>>>>> Sorry about my other post... I misread what you wanted to do. Give the
>>>>> following function a try (it is a little shorter and does less work
>>>>> than your function)...
>>>>>
>>>>> Function RoundLen(Value As Double, Length As Integer) As Double
>>>>>  Dim Num As Double
>>>>>  Dim Parts() As String
>>>>>  Num = CDbl(Format(Value, "0.##############################;;0"))
>>>>>  If InStr(Num, "E") Then
>>>>>    Parts = Split(CStr(Num), "E")
>>>>>    RoundLen = Format(Parts(0), "0." & String(Length, "0") & "E" &
>>>>> Parts(1))
>>>>>  ElseIf Num <> 0 Then
>>>>>    RoundLen = Format(Num, "0." & String(Length, "0"))
>>>>>  End If
>>>>> End Function
>>>>>
>>>>> --
>>>>> Rick (MVP - Excel)
>>>>>
>>>>>
>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>> news:%23B4vby0lJHA.1340@TK2MSFTNGP06.phx.gbl...
>>>>>> I'm sure this is more complex than necessary, but I've written the
>>>>>> following routine to handle my requirement:
>>>>>>
>>>>>> Function RoundLen(ByVal a As Double, ByVal length As Integer) As
>>>>>> String
>>>>>>   Dim g As String, x As Integer, ohs As String, neg As String
>>>>>>
>>>>>>   If a = 0 Then
>>>>>>      'Don't pad zeros. Makes them look more obvious.
>>>>>>      RoundLen = "0"
>>>>>>      Exit Function
>>>>>>   End If
>>>>>>   If a < 0 Then neg = "-"
>>>>>>   'I prefer constant length numbers with negative sign added. If
>>>>>> total length must be constant, use length = length - 1
>>>>>>   a = Abs(a)
>>>>>>   ohs = String(length, "0")
>>>>>>   x = Int(Log(a) / Log(10#))
>>>>>>   If a > CDbl(String(length, "9")) Then
>>>>>>      'For very large numbers, revert to scientific notation
>>>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>>>   ElseIf x / length < -0.5 Then
>>>>>>      'For very small numbers, revert to scientific notation. x/length
>>>>>> seems to optimize when to use it for most displayed precision
>>>>>>      g = Format$(a, "0." & Left$(ohs, length - 7) & " E+00")
>>>>>>   ElseIf a < 1 Then
>>>>>>      'Handles numbers less than 1
>>>>>>      g = Format$(a, "0." & Left$(ohs, length - 2))
>>>>>>   ElseIf x > length - 2 Then
>>>>>>      'Handles numbers of maximum length only, with decimal
>>>>>>      g = Format$(a, Left$(ohs, length))
>>>>>>   Else
>>>>>>      'Handles "normal" numbers, with 1 or more places on each side of
>>>>>> decimal.
>>>>>>      g = Format$(a, "0." & Left$(ohs, length - x - 2))
>>>>>>   End If
>>>>>>   RoundLen = neg & g
>>>>>> End Function
>>>>>>
>>>>>> Any simplification suggestions would be appreciated. ;-)
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>>
>>>>>> Rick Raisley
>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>
>>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>>> news:eal4vj0lJHA.3888@TK2MSFTNGP02.phx.gbl...
>>>>>>> It also doesn't work well with negative numbers, but thanks.
>>>>>>>
>>>>>>> --
>>>>>>> Regards,
>>>>>>>
>>>>>>> Rick Raisley
>>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>>
>>>>>>> "Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net>
>>>>>>> wrote in message news:uUAub4rlJHA.5980@TK2MSFTNGP06.phx.gbl...
>>>>>>>>A solution that does not handle scientific notation:
>>>>>>>>
>>>>>>>>    FormatNumber(strValue, 10 - Int((Log(strValue)/Log(10)) + 1))
>>>>>>>>
>>>>>>>> --
>>>>>>>> Richard Mueller
>>>>>>>> MVP Directory Services
>>>>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>>>>> --
>>>>>>>>
>>>>>>>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>>>>>>>> news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>>>>>>>>>I want to show a number as precisely as I can, using, say, 10
>>>>>>>>>characters. Something like:
>>>>>>>>>
>>>>>>>>> 12.3456789
>>>>>>>>> 1234.56789
>>>>>>>>>
>>>>>>>>> I'm using a Double for the calculations, and have thus far done
>>>>>>>>> this:
>>>>>>>>>
>>>>>>>>> '(n is a Double)
>>>>>>>>>
>>>>>>>>> Label1.Text = Left$(CStr(n), 10)
>>>>>>>>>
>>>>>>>>> That works okay except for two cases. First, it doesn't ROUND to
>>>>>>>>> those 10 digits, it truncates the text. So 0.499999999999999 reads
>>>>>>>>> as 0.49999999 instead of 0.50000000.
>>>>>>>>>
>>>>>>>>> Second, at some point with small and large numbers, I'll probably
>>>>>>>>> want to switch to Scientific notation, in order to have reasonable
>>>>>>>>> accuracy, or display very large numbers. So a Double value of
>>>>>>>>> 1.23456789E-7 would show like that, instead of 0.00000012. And
>>>>>>>>> numbers larger than 9999999999 could still be displayed.
>>>>>>>>>
>>>>>>>>> I realize I can make a series of If-Then statements, or Select
>>>>>>>>> Case, to cover all the bases. But I wondered if there was a
>>>>>>>>> simpler way of at least doing the first item. The second is easily
>>>>>>>>> covered by two statements (greater than a given number and less
>>>>>>>>> than a different given number).
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Regards,
>>>>>>>>>
>>>>>>>>> Rick Raisley
>>>>>>>>> heavymetal-A-T-bellsouth-D-O-T-net
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>>
>
Author
24 Feb 2009 8:47 PM
Rick Rothstein
Give something like this a try...

Label1.Text = CDbl(Format(n, "0.##############################"))

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>I want to show a number as precisely as I can, using, say, 10 characters.
> Something like:
>
> 12.3456789
> 1234.56789
>
> I'm using a Double for the calculations, and have thus far done this:
>
> '(n is a Double)
>
> Label1.Text = Left$(CStr(n), 10)
>
> That works okay except for two cases. First, it doesn't ROUND to those 10
> digits, it truncates the text. So 0.499999999999999 reads as 0.49999999
> instead of 0.50000000.
>
> Second, at some point with small and large numbers, I'll probably want to
> switch to Scientific notation, in order to have reasonable accuracy, or
> display very large numbers. So a Double value of 1.23456789E-7 would show
> like that, instead of 0.00000012. And numbers larger than 9999999999 could
> still be displayed.
>
> I realize I can make a series of If-Then statements, or Select Case, to
> cover all the bases. But I wondered if there was a simpler way of at least
> doing the first item. The second is easily covered by two statements
> (greater than a given number and less than a different given number).
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
>
Author
25 Feb 2009 8:40 AM
Cor Ligthert[MVP]
VB6 would be a bad program language as it was doing it with floating point
Value types in another way then you describe.

Simple don't use floating point types as you want to round.

Cor

Show quoteHide quote
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:u94X4hrlJHA.1388@TK2MSFTNGP06.phx.gbl...
>I want to show a number as precisely as I can, using, say, 10 characters.
>Something like:
>
> 12.3456789
> 1234.56789
>
> I'm using a Double for the calculations, and have thus far done this:
>
> '(n is a Double)
>
> Label1.Text = Left$(CStr(n), 10)
>
> That works okay except for two cases. First, it doesn't ROUND to those 10
> digits, it truncates the text. So 0.499999999999999 reads as 0.49999999
> instead of 0.50000000.
>
> Second, at some point with small and large numbers, I'll probably want to
> switch to Scientific notation, in order to have reasonable accuracy, or
> display very large numbers. So a Double value of 1.23456789E-7 would show
> like that, instead of 0.00000012. And numbers larger than 9999999999 could
> still be displayed.
>
> I realize I can make a series of If-Then statements, or Select Case, to
> cover all the bases. But I wondered if there was a simpler way of at least
> doing the first item. The second is easily covered by two statements
> (greater than a given number and less than a different given number).
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
>