如何将字符串“85,123kg”转换为“85,123kg”到整数85?视觉基础

发布于 2025-01-15 22:04:31 字数 556 浏览 3 评论 0原文

我正在使用 2012 Microsoft Visual Basic for Application 7.1

我有一堆字符串,例如 "85,123kg""123,05kg""11,21kg “

我想将其转换为整数,例如 8512311

我该怎么做?

现在,我尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2025-01-22 22:04:31

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 as Val("85,123kg") (or Val(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文