且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

恐慌!需要将十进制转换为整数

更新时间:2023-09-29 10:55:04

假设你的字符串最后总是为mA,首先要做的就是摆脱它。有一些不同的方式,一个正则表达式可能是最灵活的,但...如果字符串总是以mA结束然后你可以作弊。

Assuming your string always as mA at the end, the first thing to do is get rid of it. There are a load of different ways, and a Regex is probably the most flexible, but...if the string always does end "mA" then you can cheat.
string input = "0.000038mA";
double d = Convert.ToDouble(input.Substring(0, input.Length - 2));
int value = (int)(d * 1000000);



您可以跳过双重转换:


You could skip the double conversion:

string input = "0.000038mA";
int value = Convert.ToInt32(input.Substring(2, input.Length - 4));

但是......







对不起,我忘了你爱VB这么多......

But...


[edit]
Sorry, I forgot you love VB so much...

Dim input As String = "0.000038mA"
Dim d As Double = Convert.ToDouble(input.Substring(0, input.Length - 2))
Dim value As Integer = CInt(Math.Truncate(d * 1000000))






And

Dim input As String = "0.000038mA"
Dim value As Integer = Convert.ToInt32(input.Substring(2, input.Length - 4))



[/ edit]


[/edit]