Link to home
Start Free TrialLog in
Avatar of John Gates, CISSP, CDPSE
John Gates, CISSP, CDPSEFlag for United States of America

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("FineAmount").InnerText.Trim().ToString("D"))

I have also tried variants to string.format() seems to not add the decimal.  

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is what you should do

		dim str as string = "2347800"
		dim c as single=0
		Single.TryParse(str,c)
		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...
Avatar of Norie
Norie

Perhaps.
Format(node.SelectSingleNode("FineAmount").InnerText.Trim().ToString("D")/100, "0.00")

Open in new window


PS You might want to throw in a check that the value you want to format is numeric.
Avatar of John Gates, CISSP, CDPSE

ASKER

Hain

Close... But I would need

Dim str As String = "2347899"
            Dim c As Single = 0
            Single.TryParse(str, c)
            Console.WriteLine(c.ToString("#,###.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
Had to convert to VB but here is the converted for a complete answer:

Dim data As String = node.SelectSingleNode("FineAmount").InnerText.Trim()
            Dim output As Decimal

            If data.Length > 2 AndAlso IsNumeric(data) Then
                If Decimal.TryParse(String.Format("{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
John

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