复杂字符串到双精度转换

发布于 2024-12-11 20:29:13 字数 437 浏览 0 评论 0原文

我的 XML 文件中的字符串是双精度(或浮点数),例如:

<VIPair>
<voltage>+100mV</voltage>
<current>+1.05pA</current>
</VIPair>

<VIPair>
<voltage>+5.00mV</voltage>
<current>+0.0035nA</current>
</VIPair>

第一对是“0.1”伏特和“0.00000000000105”安培。 第二对是“0.005”伏特和“0.000000000035”安培。

在 C# 中如何将它们转换为双精度浮点数? 谢谢。

PS:我已经可以从 xml 文件中读取它们,目前我将它们作为字符串检索。

I have strings in a XML file that ment to be doubles (or float) such as:

<VIPair>
<voltage>+100mV</voltage>
<current>+1.05pA</current>
</VIPair>

<VIPair>
<voltage>+5.00mV</voltage>
<current>+0.0035nA</current>
</VIPair>

The first pair will be "0.1" Volt and "0.00000000000105" Ampere.
The second pair would be "0.005" Volt and "0.000000000035" Ampere.

How can I convert them to double of float in C#?
Thanks.

P.S: I already can read them from xml file and at the moment I retrive them as string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

长梦不多时 2024-12-18 20:29:13

尝试用这个:

// Read string (if you do not want to use xml)
string test = "<voltage>+100mV</voltage>";
string measure = test.Substring(test.IndexOf('>')+1);
measure = measure.Substring(0, measure.IndexOf('<')-1);

// Extract measure unit
string um = measure.Substring(measure.Length - 1);
measure = measure.Substring(0, measure.Length - 1);
// Get value
double val = Double.Parse(measure);
// Convert value according to measure unit
switch (um)
{
    case "G": val *= 1E9; break;
    case "M": val *= 1E6; break;
    case "k": val *= 1E3; break;
    case "m": val /= 1E3; break;
    case "u": val /= 1E6; break;
    case "n": val /= 1E9; break;
    case "p": val /= 1E12; break;
}

Try with this:

// Read string (if you do not want to use xml)
string test = "<voltage>+100mV</voltage>";
string measure = test.Substring(test.IndexOf('>')+1);
measure = measure.Substring(0, measure.IndexOf('<')-1);

// Extract measure unit
string um = measure.Substring(measure.Length - 1);
measure = measure.Substring(0, measure.Length - 1);
// Get value
double val = Double.Parse(measure);
// Convert value according to measure unit
switch (um)
{
    case "G": val *= 1E9; break;
    case "M": val *= 1E6; break;
    case "k": val *= 1E3; break;
    case "m": val /= 1E3; break;
    case "u": val /= 1E6; break;
    case "n": val /= 1E9; break;
    case "p": val /= 1E12; break;
}
白日梦 2024-12-18 20:29:13

您好,这是 Marco 所写内容的另一个版本。

            string str = "1pV";
            double factor;
            double value;
            switch (str[str.Length-2])
            {
                case 'M': factor = 1E6; break;
                case 'm': factor = 1E-3; break;
                case 'n': factor = 1E-9; break;
                case 'p': factor = 1E-12; break;
                default:
                    factor = 1; break;
            }
            value = double.Parse(str.Substring(0,str.Length-2)) * factor;

假设您已经可以使用 html 文本。我尝试用一​​个子字符串做同样的事情,用字符而不是字符串切换大小写(这比比较字符串要快一些)和 double.parse。希望有人能想出比这个更好的版本。

Hi here is another version of what Marco has written.

            string str = "1pV";
            double factor;
            double value;
            switch (str[str.Length-2])
            {
                case 'M': factor = 1E6; break;
                case 'm': factor = 1E-3; break;
                case 'n': factor = 1E-9; break;
                case 'p': factor = 1E-12; break;
                default:
                    factor = 1; break;
            }
            value = double.Parse(str.Substring(0,str.Length-2)) * factor;

Assuming that the html text is already available to you. I have tried to do the same thing with one substring, switch case with characters instead of strings(This is a bit faster to comparing strings) and a double.parse. Hope someone comes up with a better version than this.

放我走吧 2024-12-18 20:29:13

使用 string.Substringstring.Remove() 方法删除后缀字符串 mVnA 并使用 double.TryParse() 方法将字符串解析为双精度。

Remove the suffix string mV and nA using string.Substring or string.Remove() method and use double.TryParse() method to parse string to double.

德意的啸 2024-12-18 20:29:13

如果您的值末尾总是有 2 个字符,您可以简单地删除它们并解析数字。

var newstring = fullstring.Substring(0, fullstring.Length - 2);
var number = double.Parse(newstring);

If your values always have 2 chars on the end you could simple remove these and parse the number.

var newstring = fullstring.Substring(0, fullstring.Length - 2);
var number = double.Parse(newstring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文