Rounding Text Values problem

Discussion in 'AutoCAD' started by ajtruckle, Mar 4, 2005.

  1. ajtruckle

    ajtruckle Guest

    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
     
    ajtruckle, Mar 4, 2005
    #1
  2. ajtruckle

    Jackrabbit Guest

    strText = Format(objCoord(2), "0.00")
     
    Jackrabbit, Mar 4, 2005
    #2
  3. 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)
    
    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".
    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"
     
    James Belshan, Mar 4, 2005
    #3
  4. ajtruckle

    ajtruckle Guest

    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?
     
    ajtruckle, Mar 4, 2005
    #4
  5. ajtruckle

    ajtruckle Guest

    Interesting, thanks
     
    ajtruckle, Mar 4, 2005
    #5
  6. "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

    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...
     
    James Belshan, Mar 4, 2005
    #6
  7. ajtruckle

    KCCadDude Guest

    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
     
    KCCadDude, Mar 5, 2005
    #7
  8. ajtruckle

    Jon Guest

    format(objCoord(2), "0." & string$(iDecPlaces,"0"))

    Jon
     
    Jon, Mar 6, 2005
    #8
  9. ajtruckle

    tp Guest

    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
     
    tp, Mar 8, 2005
    #9
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 (here). After that, you can post your question and our members will help you out.