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

Posted on 2011-03-03
Medium Priority
Last Modified: 2012-08-14

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

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?

Question by:Letsgetcoding
  • 3
LVL 42

Accepted Solution

Meir Rivkin earned 2000 total points
ID: 35025677
Dim num As String = "12.454353"
Dim s As String = Format(Double.Parse(num), "#.##")

LVL 42

Expert Comment

by:Meir Rivkin
ID: 35025695
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"

Author Comment

ID: 35026256
Is it possible to do it without creating another string? For example
Dim num As String = "12.454353"
num = Format(Double.Parse(num), "#.##")
LVL 42

Expert Comment

by:Meir Rivkin
ID: 35026294


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question