Issues on Convert String to Double

  • Thread starter Thread starter Peter Newman
  • Start date Start date
P

Peter Newman

Earlier i posted a problem and both replies worked, however I have come up
with a new problem when testing

CDbl("00000001234") * 0.01 AND Double.Parse("00000001234") * 0.01 both return
12.34 which is correct, however
CDbl("00000001200") * 0.01 AND Double.Parse("00000001200") * 0.01 both return
12

I need to be returned 12.00

The only way i know how to do this is to minuplate the string and add the
decimal point manually, unless anyone knows of a better way
 
Peter said:
Earlier i posted a problem and both replies worked, however I have
come up with a new problem when testing

CDbl("00000001234") * 0.01 AND Double.Parse("00000001234") * 0.01
both return
12.34 which is correct, however
CDbl("00000001200") * 0.01 AND Double.Parse("00000001200") * 0.01
both return 12

I need to be returned 12.00

The only way i know how to do this is to minuplate the string and add
the decimal point manually, unless anyone knows of a better way

12.00 is the same value as 12. If you want to convert back to a string with
2 decimal places, you have to use the correct format.

dim d as double = 12
dim s as string = d.tostring("0.00") 'two decimal places


Armin
 
Armin,

If i give the full line of code it may explain what i amd trying to achieve.
I am reading text files conatining thousands of banking transactions, and
importing them into SQL datasets vis a SP. A typical line in the file will
contain 101 bytes of information and I am just stripping out the required
data as follows

Line of code has been modified for a console.writeline by adding & "," &

Console.WriteLine(VB.Right("000000" & Mid(ImportLine, 1, 6),
6) & "," & VB.Right("00000000" & Mid(ImportLine, 7, 8), 8) & "," &
RTrim(VB.Left(Mid(ImportLine, 83, 18) & Space(18), 18)) & "," &
RTrim(VB.Left(Mid(ImportLine, 65, 18) & Space(18), 18)) & "," &
Double.Parse(VB.Right("00000000000" & Mid(ImportLine, 36, 11), 11)) * 0.01 &
"," & Mid(ImportLine, 16, 2) & "," & "IMPORTED")


The section section of the importline referred in -
Double.Parse(VB.Right("00000000000" & Mid(ImportLine, 36, 11), 11)) * 0.01 is
11 bytes ie "00000001000" which needs to parse as 10.00 . I hope this
explains things a bit clearer
 
Hello Peter,

I assume you want to output the value. For this a double is not the best
idea as you need a string for that. So, you need to convert it back into
a string.

Dim MyDbl As Double
Dim MyString As String

MyDbl = CDbl("00000001200") * 0.01
MyString = MyDbl.ToString("0.00")
'If you like the more classic approach, you could also
'use: MyString = Format(MyDbl,"0.00")
MsgBox(MyString)



Best regards,

Martin
 
Peter said:
The section section of the importline referred in -
Double.Parse(VB.Right("00000000000" & Mid(ImportLine, 36, 11), 11)) *
0.01 is 11 bytes ie "00000001000" which needs to parse as 10.00 . I
hope this explains things a bit clearer

Yes, I read also your other thread, but which problem do you have now? If
the line contains "00000001000", the result is 10, which is the same value
as 10.0, 10.00, 10.000 and 10.0000. A double value, which is the result of
your operation, does not have trailing decimal zeros. You have to convert it
to a String as I've shown you.


Armin
 
Peter said:
If i give the full line of code it may explain what i amd trying to
achieve. I am reading text files conatining thousands of banking
transactions, and importing them into SQL datasets vis a SP.

It is likely that you should be using the decimal type and not a double.

Andrew
 
Back
Top