乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 134|回复: 11

VBA: Number formatting

[复制链接]

46

主题

118

帖子

23

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
291
发表于 2022-7-6 22:27:16 | 显示全部楼层 |阅读模式
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")
  1. '*************************************************'************ 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"   [b]' Display level figure on screen..LVLvalueGRND = LVLvalueGRND / 1000   'Get it into metres, no mm..LVLvalueGRND = Round(LVLvalueGRND, 3)LVLvalueGRND = Format(LVLvalueGRND, "0.000")[/b]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
回复

使用道具 举报

1

主题

80

帖子

73

银币

初来乍到

Rank: 1

铜币
16
发表于 2022-7-6 22:36:29 | 显示全部楼层
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.
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 22:40:17 | 显示全部楼层
 
To get the trailing zeros you need to rewrite the above line so it reads:
 
  1. LVLvalueGRND = Format(LVLvalueGRND, "0.##0")
I hope that helps
回复

使用道具 举报

1

主题

80

帖子

73

银币

初来乍到

Rank: 1

铜币
16
发表于 2022-7-6 22:44:09 | 显示全部楼层
 
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.
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 22:51:09 | 显示全部楼层
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 solution  try it out and get back to us with your result.
回复

使用道具 举报

1

主题

80

帖子

73

银币

初来乍到

Rank: 1

铜币
16
发表于 2022-7-6 22:56:51 | 显示全部楼层
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
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 23:04:13 | 显示全部楼层
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.
回复

使用道具 举报

46

主题

118

帖子

23

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
291
发表于 2022-7-6 23:06:16 | 显示全部楼层
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
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 23:13:22 | 显示全部楼层
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.
回复

使用道具 举报

46

主题

118

帖子

23

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
291
发表于 2022-7-6 23:20:11 | 显示全部楼层
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..
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-12 23:06 , Processed in 0.598694 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表