VBA: Number formatting
Hey all,I appreciate this is more VB than VBA but its an AutoCAD tool i'm making so posting it here too..
Trying to creating a tool that inserts datum lines in a drawing which is fine, the only issue is the formatting of the attribute to display within the block (see the bold part of the code below). I'm trying to display the datum level as "+0.000" which is fine for most input but if the number has any trailing zeros, they are not displayed. I've searched high and low on the net for this and have tried everything i have found but still no results..
(All variables in the code have been declared and no errors occur at run-time, just not the correct display:
ie: "+6.800" displays in the block as "+6.8" and "+8.760" displays as "+8.76")
'*************************************************'************ GROUND FLOOR ***************'*************************************************GROUND:' Check if GROUND is selected..If LVLtxtGRND.Text = "" Then GoTo FIRST Else 'If a value is shown then insert the block.. LVLvalueGRND = LVLtxtGRND.Text ' Draw LINE for level.. LineGRNDstart(0) = 0: LineGRNDstart(1) = LVLvalueGRND: LineGRNDstart(2) = 0 LineGRNDend(0) = LINEend: LineGRNDend(1) = LVLvalueGRND: LineGRNDend(2) = 0 Set LINEgrnd = ThisDrawing.ModelSpace.AddLine(LineGRNDstart, LineGRNDend) LINEgrnd.Layer = "X-Levels" ' Insert LEVEL Block.. LVLinsertGRND(0) = 0: LVLinsertGRND(1) = LVLvalueGRND: LVLinsertGRND(2) = 0 Set LVLblockGRND = ThisDrawing.ModelSpace.InsertBlock(LVLinsertGRND, "Level", 250#, 250#, 250#, 0) LVLblockGRND.Layer = "X-Notes" ' Display level figure on screen..LVLvalueGRND = LVLvalueGRND / 1000 'Get it into metres, no mm..LVLvalueGRND = Round(LVLvalueGRND, 3)LVLvalueGRND = Format(LVLvalueGRND, "0.000")AttribZ = LVLblockGRND.GetAttributes ' Get Block attributes.. For CountX = LBound(AttribZ) To UBound(AttribZ) Select Case AttribZ(CountX).TagString Case "LEVEL" AttribZ(CountX).TextString = PlusChar & LVLvalueGRND & " " & " " & DESCcomboGRND.Text End SelectNext CountX'On Error Resume Next' MsgBox Err.Number & vbCr & Err.Description, vbExclamation, "Error:"End If'*************************************************'************ GROUND FLOOR ***************'*************************************************
Any ideas? This is twisting my melon now grrr Baffling. Shouldn't need to do this but as a real kludge until something better comes along you could compare the INSTR position of the decimal point with the string's length and append any needed zeroes. If no decimal point append ".000" to the string.
To get the trailing zeros you need to rewrite the above line so it reads:
LVLvalueGRND = Format(LVLvalueGRND, "0.##0")I hope that helps
If that works we all learn something. I hope someone can explain why it should work, either by example or by arguing from first principles. I've used it a lot for this very situation in both VBA and VB .NET and it works fine. It was a real pain in the backside for me until I found the solutiontry it out and get back to us with your result. It works for sure. Sorry, I was just idly curious as to how you came upon the answer - it's not well documented by MS e.g:
http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.90).aspx Glad it worked for you hugha.
You are right it is not very well documented and I saw a similar link to the one you just posted, after which I just experimented a bit till I got it to work. The # symbol is a standard place holder in Visual Basic, although the documentation says that "0.000" should work, but it never did for me. I have been using it regularly now for around six years. Let's hope that it works for hardwired too
Thanks for getting back hugha and
that we have all learned a little bit. Hey all,
Thanks for all your replies. I tried the '0.##0' solution, Tyke but still no luck. I think my PC doesn't like to be too negative haha. This is a real head-screwer as it seems to be working for everyone else just not me. I'll give your first solution a go, hugha and see what happens I've tried out exactly the code you have on two computers, in AutoCAD 2010 and 2013 in both German and English language versions of AutoCAD, but admittedly only on a German Windows XP operating system. It works fine with your unaltered code and also with my suggestion.
What OS do you have? 32 bit or 64 bit? It shouldn't make any difference, but check the decimal seperator in your Regional Settings. Hey Tyke, I've had a look at my regional settings and can't see anything else to change except figures to 3 decimal places but that didn't work anyway..
I'm on AutoCAD 2010 on an 32-bit XP Pro machine by the way..
页:
[1]
2