VB.Net Format string into currency (56.4543534) to (56.45)

Hey,

How would I go about changing a string into this format:
56.45

I have tried String.Format("{0:C}", Cost), but it still comes out with more than two after decimal numbers.

I have a lot of strings that contain numbers, so converting each to a double would be a lengthy process. Any easy ways?

Thanks!
Jessee
LetsgetcodingAsked:
Who is Participating?
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
vb.net:
Dim num As String = "12.454353"
Dim s As String = Format(Double.Parse(num), "#.##")

0
 
Meir RivkinFull stack Software EngineerCommented:
either this, or using string manipulation:
        Dim num As String = "12.454353"
        Dim s As String = num.Substring(0, num.IndexOf(".") + 3)
s --> "12.45"
0
 
LetsgetcodingAuthor Commented:
Is it possible to do it without creating another string? For example
Dim num As String = "12.454353"
num = Format(Double.Parse(num), "#.##")
0
 
Meir RivkinFull stack Software EngineerCommented:
@Letsgetcoding
yes.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.