John Gates, CISSP, CDPSE
asked on
vb.net and format a number assuming always two decimals from the right
What is the best way to take a string (let's say 99900) and string it so it equals 999.00 ? format for some reason I am struggling with.. I think I am overthinking it but looking for other ideas.
CInt(node.SelectSingleNode ("FineAmou nt").Inner Text.Trim( ).ToString ("D"))
I have also tried variants to string.format() seems to not add the decimal.
Thanks in advance!
CInt(node.SelectSingleNode
I have also tried variants to string.format() seems to not add the decimal.
Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Perhaps.
PS You might want to throw in a check that the value you want to format is numeric.
Format(node.SelectSingleNode("FineAmount").InnerText.Trim().ToString("D")/100, "0.00")
PS You might want to throw in a check that the value you want to format is numeric.
ASKER
Hain
Close... But I would need
Dim str As String = "2347899"
Dim c As Single = 0
Single.TryParse(str, c)
Console.WriteLine(c.ToStri ng("#,###. 00"))
that number to end in .99
so the scenerio I am dealing is this.. This provider I am dealing with is sending me $7.99 as 799
I need to assume any value passed the last two numbers would be cents always.. I am feeling a little better about being stuck on this now... Thoughts on that?
-J
Close... But I would need
Dim str As String = "2347899"
Dim c As Single = 0
Single.TryParse(str, c)
Console.WriteLine(c.ToStri
that number to end in .99
so the scenerio I am dealing is this.. This provider I am dealing with is sending me $7.99 as 799
I need to assume any value passed the last two numbers would be cents always.. I am feeling a little better about being stuck on this now... Thoughts on that?
-J
ASKER
Had to convert to VB but here is the converted for a complete answer:
Dim data As String = node.SelectSingleNode("Fin eAmount"). InnerText. Trim()
Dim output As Decimal
If data.Length > 2 AndAlso IsNumeric(data) Then
If Decimal.TryParse(String.Fo rmat("{0}. {1}", data.Substring(0, data.Length - 2), data.Substring(data.Length - 2)), output) Then
Console.WriteLine("Data converted - {0}", output)
Else
Console.WriteLine("Could not convert data - {0}", data)
End If
End If
Thank you for your help on this!!!!
-J
Dim data As String = node.SelectSingleNode("Fin
Dim output As Decimal
If data.Length > 2 AndAlso IsNumeric(data) Then
If Decimal.TryParse(String.Fo
Console.WriteLine("Data converted - {0}", output)
Else
Console.WriteLine("Could not convert data - {0}", data)
End If
End If
Thank you for your help on this!!!!
-J
John
Just curious, what if the provider is sending a value less than a dollar, eg $0.99 as 99?
Just curious, what if the provider is sending a value less than a dollar, eg $0.99 as 99?
I need to assume any value passed the last two numbers would be cents always.. I am feeling a little better about being stuck on this now... Thoughts on that?
but I told you above:
* you may add "c=c/100" after line 3 if you need...
dim str as string = "2347800"
dim c as single=0
Single.TryParse(str,c)
c=c/100
console.WriteLine(c.ToString("#,###.00"))
Open in new window
>>> 2,347,800.00
* you may add "c=c/100" after line 3 if you need...