Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

The problem I have is I need to convert a decimal to an integer, if I have the reading from an instrument of 0.000038mA the genius who wrote this mess compare it to two values read from ini file 19 & 50, 19 & 50 mA I assume, it always fails so I need a way of converting 0.000038mA to 38, (I have tried sticking 0.000019 in the ini file and that does not work) Convert.To doesn't help!! Help Gimme Codez !!!! I was looking at doing by converting to a string and substring out the values, however I can't be sure of the number of decimals....
Glenn (Hmm looking at his code again!)

BoardCurrentMeasuredString2 = BoardCurrentMeasuredString.Substring(BoardCurrentMeasuredString.Length - 2, BoardCurrentMeasuredString.Length) well that doesn't work! try the next idea....
here it lands
VB
' MsgBox(Convert.ToString(BoardCurrentMeasured))
BoardCurrentMeasuredString = BoardCurrentMeasured.ToString()
MsgBox(BoardCurrentMeasuredString)
BoardCurrentMeasuredString.Trim()

StartPointString = (Val(BoardCurrentMeasuredString.Length - 2))
MsgBox(BoardCurrentMeasuredString.Length)
StopPointString = (Val(BoardCurrentMeasuredString.Length))
MsgBox(BoardCurrentMeasuredString.Length)

BoardCurrentMeasuredString2 = BoardCurrentMeasuredString.Substring(Convert.ToInt16(BoardCurrentMeasuredString.Length - 2), Convert.ToInt16(BoardCurrentMeasuredString.Length))
MsgBox(BoardCurrentMeasuredString2)


MsgBox(BoardCurrentHighVal)
MsgBox(BoardCurrentLowVal)
'BoardCurrentRead()

Bad Words time for Christmas Lunch!!!
See Ya
Posted
Updated 20-Dec-13 1:35am
v3

1 solution

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.
C#
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:
C#
string input = "0.000038mA";
int value = Convert.ToInt32(input.Substring(2, input.Length - 4));
But...


[edit]
Sorry, I forgot you love VB so much...
VB
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
VB
Dim input As String = "0.000038mA"
Dim value As Integer = Convert.ToInt32(input.Substring(2, input.Length - 4))

[/edit]
 
Share this answer
 
v2
Comments
glennPattonWork3 20-Dec-13 7:36am    
Thanks Griff.... Happy Tree Day!!!!
OriginalGriff 20-Dec-13 8:07am    
Indeed - A very Merry Artificial Shrub with LED lights on to you too!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900