Rounding Text Values problem

Rounding Text Values problem

Anonymous
Not applicable
782 Views
8 Replies
Message 1 of 9

Rounding Text Values problem

Anonymous
Not applicable
I use this code:

' Create text string (rounded to decimal places)
strText = ThisDrawing.Utility.RealToString(objCoord(2), _
acDecimal, iDecPlaces)

to round of numerical values to a certain number of decimal places. But it does not always work. Imagine I want to round to 2 decimal places and the value is 1.5 then the string returned is "1.5" and NOT "1.50".

I can't seem to work out how to get it to add the required zeroes to ensure it is to the right decimal places, apart from a manual test on the string returned and working it out myslef and adding the "0" until there. Even worse, a value like 12 would return "12" and not "12.00"

Any ideas?

Andrew
0 Likes
783 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
strText = Format(objCoord(2), "0.00")
0 Likes
Message 3 of 9

Anonymous
Not applicable
Here's one way... [code] Dim valu As Double: valu = 12 Dim numDec As Integer: numDec = 2 Dim formString As String If numDec <= 0 Then formString = "#" Else formString = "#." & String(numDec, "0") End If MsgBox Format(valu, formString) [/code] "ajtruckle" wrote in message news:221977.1109941450385.JavaMail.jive@jiveforum2.autodesk.com... > I use this code: > > ' Create text string (rounded to decimal places) > strText = ThisDrawing.Utility.RealToString(objCoord(2), _ > acDecimal, iDecPlaces) > > to round of numerical values to a certain number of decimal places. But it does not always work. Imagine I want to round to 2 decimal places and the value is 1.5 then the string returned is "1.5" and NOT "1.50". > > I can't seem to work out how to get it to add the required zeroes to ensure it is to the right decimal places, apart from a manual test on the string returned and working it out myslef and adding the "0" until there. Even worse, a value like 12 would return "12" and not "12.00" > > Any ideas? > > Andrew
0 Likes
Message 4 of 9

Anonymous
Not applicable
But your sample assumes 2 decimal places, the "0.00" format string. My decimal places is set by the user so I have to programatically create the format string, right?

And should it not be #. as opposed to 0.?? Format number confuses me. Our numbers should atleast be 0.xxxxx or larger, like 10.xxxx or 200.xxxx etc.. but they will all change, so the format string can't be hardcoded into the function call...

Thoughts?
0 Likes
Message 5 of 9

Anonymous
Not applicable
Interesting, thanks
0 Likes
Message 6 of 9

Anonymous
Not applicable
"0." is fine for the left side of the decimal. The string will expand to hold values > 9. The only difference between "#" and "0" is that for numbers which come out shorter than the format string, "0" will stay in place, but "#" will disappear (unless they are required to stay as placeholders for a padded zero, as in the third Debug.Print. Debug.Print Format(9.9, "####.####") 9.9 Debug.Print Format(9.9, "0000.0000") 0009.9000 Debug.Print Format(9.9, "0###.###0") 0009.9000 Debug.Print Format(9.9, "##00.00##") 09.90 > And should it not be #. as opposed to 0.?? Format number confuses me. Our numbers should atleast be 0.xxxxx or larger, like 10.xxxx or 200.xxxx etc.. but they will all change, so the format string can't be hardcoded into the function call... > > Thoughts?
0 Likes
Message 7 of 9

Anonymous
Not applicable
Here's an idea. Test the string for a decimal. If the isn't one then concatenate the integer with ".00". So "3" & ".00" would give you "3.00". Now if there is a decimal find it's location with InStr. Add 2 to that for the preferred string length. concatenate the string with a "0" at the end. Then use "Left" with the preferred string length. Say the string is "3.1". The code might look like this
MyString = left(OriginalString & "00000000",(instr(OriginalString,".")+2))

If OriginalString = 3.1 then MyString = 3.10
If OriginalString = 3.12 then MyString = 3.12
If OriginalString = 3.12892323 then MyString = 3.12
0 Likes
Message 8 of 9

Anonymous
Not applicable
format(objCoord(2), "0." & string$(iDecPlaces,"0")) Jon
0 Likes
Message 9 of 9

Anonymous
Not applicable
Function NumberToString(ByVal Value As Double, ByVal Unit As AcUnits, _ ByVal precision As Integer) As String Dim dimzin As Long With ThisDrawing dimzin = .GetVariable("dimzin") .SetVariable "dimzin", 0 NumberToString = .Utility.RealToString(Value, Unit, precision) .SetVariable "dimzin", dimzin End With End Function
0 Likes