Solved

vb,net convert money to string

Posted on 2014-12-06
3
223 Views
Last Modified: 2014-12-06
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
Comment
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
  • Learn & ask questions
  • 2
3 Comments
 
LVL 37

Accepted Solution

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

Open in new window


58.9800 would give "58.98"
0
 
LVL 34

Expert Comment

by:it_saige
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")

Open in new window


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"))
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output -Capture.JPG
-saige-
0
 
LVL 37

Expert Comment

by:Neil Russell
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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