How to convert string to decimal?

  • Thread starter Thread starter JenHu
  • Start date Start date
J

JenHu

Hi all,

I have to read from a text file and generate values and insert to
database.
But first of all, when the text file contains '0000000000', I received
a sEfundAmt value = 0D instead of 0.0 on

sEfundAmt = Decimal.Parse(Mid$(sRetLine, 30, 10)) / 100.0

For example, in the text file, value is '0000150776', I want to
convert it to 1507.76,
if value is '0000000000', the sEfundAmt should be 0.0

I tried Int32.Parse (Mid$(sRetLine, 30, 10)) / 100.0 , and the result
is 0.0, but whenever it is tried to convert to decimal, the result is
not correct. How to get around it?

Thank you.

Here is the partial code:
-----------------------------------------------------------
Dim sr As New StreamReader(savePath & filename)
Dim sRetLine As String
Dim sEfundAmt As Decimal
Try
While sr.Peek > -1
sRetLine = sr.ReadLine

sEfundAmt = Decimal.Parse(Mid$(sRetLine, 30, 10)) /
100.0

Catch err As Exception
message.Text = err.Message
End Try

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
try following code:

Dim strVal String = "0000150776";
Dim doubleVal As Double = Val(str) / 100

Elton Wang
(e-mail address removed)
-----Original Message-----
Hi all,

I have to read from a text file and generate values and insert to
database.
But first of all, when the text file
contains '0000000000', I received
 
The value inside the decimal is correct. You are concerned with the
formatting of the value. 0 is always 0. And 0.0 still have the "value" of
0.

Try adding a custom string format to the value. This will make it appear
exactly how you want it to appear.

Console.WriteLine( sEfundAmt.ToString( "##0.00;-##0.00;0.0" ) )

This format string is broken up into positive representation ; negative
representation ; zero representation.

Here is a link that covers the various string formatting you can do with
numeric data types and their ToString methods.

http://msdn.microsoft.com/library/d...uide/html/cpconcustomnumericformatstrings.asp

HTH,

bill
 
Quite a few compilation errors in that code....

anyways, if you want a double, that'll work, if you want a decimal (as
requested), you'll need to do:
Decimal.Parse(x) / 100D

Karl
 
Back
Top