如何将字符串“85,123kg”转换为“85,123kg”到整数85?视觉基础
我正在使用 2012 Microsoft Visual Basic for Application 7.1
我有一堆字符串,例如 "85,123kg"
、 "123,05kg"
、"11,21kg “
。
我想将其转换为整数,例如 85
、123
、11
我该怎么做?
现在,我尝试 For
每个字符串,并且当 ","
检测到 - Exit for
以及 "," 之前的所有数字
添加到新字符串
但这看起来很糟糕
If InStr(text, "kg") > 0 Then
For charIndex = 1 To text.Length
If text(charIndex) Like "," Then
Exit For
Else
I'm using 2012 Microsoft Visual Basic for Application 7.1
I have a bunch of strings like "85,123kg"
, "123,05kg"
, "11,21kg"
.
And I want to convert it to Integers like 85
, 123
, 11
How can I do it?
Now I'm trying to For
every string and when ","
detects - Exit for
And all numbers before the ","
add to new string
But this looks awfull
If InStr(text, "kg") > 0 Then
For charIndex = 1 To text.Length
If text(charIndex) Like "," Then
Exit For
Else
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Microsoft Visual Basic for Application 7.1 有一个名为
Val
的函数来完成此任务。您可以将其用作Val("85,123kg")
(或Val(text)
,其中 text 等于“85,123kg”)并且获取整数 85。Val
函数尝试从字符串左侧开始提取数字。它将忽略空白(空格和制表符)和换行符。读取在第一个无法识别为数字一部分的字符处停止。通常被视为数值一部分的符号和字符(例如美元符号和逗号)无法识别。
Microsoft Visual Basic for Application 7.1 has a function named
Val
that does exactly that. You would use it asVal("85,123kg")
(orVal(text)
where text is equal to "85,123kg") and obtain the integer 85.The
Val
function tries to extract a number from the string starting from the left. It will ignore whitespace (spaces and tabs) and linefeeds.Reading stops at the first character that cannot be recognized as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized.