How to convert a number to Fomated text

C

Chris H

I need to write a number as a fixed length of text for example

-3.74987654321987654321 would be -00003.750000
and
+3.74987654321987654321 would be +00003.750000

I can't find any commands to allow me to format the text representation of
the number.

Thanks In Advance

Chris
 
G

Graham Mayor

In vba it would be e.g.

Dim sNum As String
sNum = -3.74987654321988
sNum = format(sNum, "+00000.00;-00000.00") & "0000"
MsgBox sNum

in Word you could use a formula field eg
{ = -3.74987654321987654321 \# "+00000.00;-00000.00" }0000

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jay Freedman

I can't find any commands to allow me to format the text

Oh really? How about the Format function? Since by your examples you also
want to round the value to two decimal places before formatting it, the
following example shows the use of the Round function as well.

Sub x()
Dim s1 As String, s2 As String
Dim n As Double

n = 4# * Atn(1#) ' pi
s1 = RoundAndFormat(n)
s2 = RoundAndFormat(-n)

MsgBox "Original number: " & n & _
vbCr & "Formatted pos: " & s1 & _
vbCr & "Formatted neg: " & s2
End Sub

Private Function RoundAndFormat(n As Double) As String
Dim s As String
s = Format(Round(n, 2), "00000.000000")
If n >= 0 Then s = "+" & s
RoundAndFormat = s
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top