Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am reading in a number from a device and am not handling the exponent properly.
the number is for example -9.76033e-1 (this is an Ammeter I am reading) I need to convert this to 976.033 for display as milliamps. While I could convert it by moving the decimal point before converting it. A rough demo of what I am try to achieve is below:
VB
Dim Value_Eng As String
Dim Value As Decimal

Value_Eng = "-9.76033e-1"
MsgBox(Value_Eng)

Value = Convert.ToInt16(Value_Eng)
MsgBox(Value)

It takes -9.76033e-1 and converts it to -976.033 This returns an "Input string was not in the correct format"

I would have though there would be a prewritten function for converting bases...or is it a vain hope!

Glenn
<<<<<<update>>>>>>
I have found an answer String.Format example below
VB
Dim Value_Eng As String
               Dim Value As Decimal

               Value_Eng = "-9.76033e-1"
               MsgBox(Value_Eng)
               Value = String.Format(Value_Eng)

               MsgBox(Value)

Can some one give there views on this please as all I would have to do is multiply by 1000!
Thanks Again
Glenn
Posted
Updated 9-Apr-14 6:17am
v3

This is a better way:
VB.NET
Value = Decimal.Parse(Value_Eng, Globalization.NumberStyles.Float) ' now, multiply by 1000


Or, if you are not 100% sure that the string will be in a valid format, use the TryParse[^] method:
VB
If Decimal.TryParse(Value_Eng, Globalization.NumberStyles.Float, Globalization.CultureInfo.InvariantCulture, Value) Then
    ' can parse the value, now multiply by 1000
Else
    ' cannot parse the value
End If
 
Share this answer
 
v2
Comments
glennPattonWork3 9-Apr-14 12:31pm    
Like a charm! Thanks You very much!
Thomas Daniels 9-Apr-14 12:32pm    
You're welcome!
glennPattonWork3 9-Apr-14 12:35pm    
It always gets me in a mess have I accepted your answer or not?
Thomas Daniels 9-Apr-14 12:36pm    
Yes, you have.
Try this:
VB
Dim Value_Eng As String = "-9.76033e-1"
Dim Value As Decimal
If Decimal.TryParse(Value_Eng, System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture, Value) Then
    Console.WriteLine(Value * 1000)
End If
 
Share this answer
 
Comments
glennPattonWork3 9-Apr-14 12:31pm    
Thanks Griff, though I could count on you,, one thing the CultureInfo requires more set up
Thomas Daniels 9-Apr-14 12:34pm    
+5!

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