Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# vb,net convert money to string

Posted on 2014-12-06
Medium Priority
261 Views
Hi experts
I have a table field hour format money and it has a value 5.4800 stored to it,
I would like convert that to a string but only 2 decimals i,e 5.48
I am using the following code to get this value. I was wondering if their is an easy and shorter way to doing it,

Dim hoursxx = row("Hours").ToString
Dim lhx = Len(hoursxx)
Dim lhxx = Microsoft.VisualBasic.Left(hoursxx, lhx - 2)

lhxx returns 5.68

thanks
0
Question by:b001
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 37

Accepted Solution

Neil Russell earned 2000 total points
ID: 40484977
``````Dim hoursxx = row("hours")
Dim str1 as string
str1 = Format(hoursxx, "##,##0.00")
``````

58.9800 would give "58.98"
0

LVL 34

Expert Comment

ID: 40484990
Whenever doing these types of conversions, it is better to do a type and exception safe conversion, in your case:
``````Dim hoursxx = row("hours")
Dim formatter As Decimal
Dim converted As String = If(Decimal.TryParse(hoursxx, formatter), formatter.ToString("0.00"), "Invalid decimal/money value")
``````

Example program -
``````Module Module1
Private money1 As String = "58.98000"
Private money2 As String = "145.00"
Private money3 As String = "45.789032"
Private money4 As String = "I am not money"

Sub Main()
Dim format As Decimal = Decimal.MinValue
Console.WriteLine("Money 1 is - {0}", If(Decimal.TryParse(money1, format), format.ToString("0.00"), "Not a decimal/money value"))
Console.WriteLine("Money 2 is - {0}", If(Decimal.TryParse(money2, format), format.ToString("0.00"), "Not a decimal/money value"))
Console.WriteLine("Money 3 is - {0}", If(Decimal.TryParse(money3, format), format.ToString("0.00"), "Not a decimal/money value"))
Console.WriteLine("Money 4 is - {0}", If(Decimal.TryParse(money4, format), format.ToString("0.00"), "Not a decimal/money value"))
End Sub
End Module
``````

Produces the following output -
-saige-
0

LVL 37

Expert Comment

ID: 40485008
@it_saige
If you read the question, the data was coming from a Monet type database field. So it is not going to contain "I am not money" hence I never made assumptions about type and gave what was required for this scenario.
Had the source of the data been unknown then yes, yours would be safe.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month7 days, 21 hours left to enroll