Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some help with some .net code. I'm bringing a number from a access database field in to a textbox on a form and formating it as currency so it will display the $ and proper comma placement and two decimal points.

editClaim_Amount.Text=Format(dsEnterData.Tables(0).Rows(0).Item("Claim_Amount", "c")


That part is good - now I need to be able to save that text back to its origanal format.

Example: Input 23468
Display:$23,468.00
Ssave:23468

It seems simple but I'm stuck. I can trim off the $ and .00 but not the comma(s). Is there a straightforward way to "unformat"?

Thanks in advance for any help

Craig
Posted
Updated 22-Sep-10 8:37am
v2

Use a masked edit textbox to format the string appropriately rather than the formatting the text before assigning it. Then you can get the raw value ,without any formatting, when necessary.
 
Share this answer
 
Comments
Sandeep Mewara 23-Sep-10 6:04am    
Comment from OP:
Thanks for the suggestion Mark.
That would be a workable solution if the database didn't already exist - it does with about 6,000 records so I need to deal with what I have and the request - have the dollar amounts display in the in the form used to edit a record from the database and save the results back to the database
[no name] 23-Sep-10 7:12am    
What does existing records have to do with it? Correct the mistake and do it correctly. Formatting information should never be included in a database field.
[no name] 23-Sep-10 7:23am    
Regex.Replace(input, "[$-.]", "")
I had to tweak it a little to remove the decimal point and the number folowing it which are always 0 it works great otherwise. Thanks so much for your help.
Craig C.
 
Share this answer
 
I think you would like this.. :thumbsup:

VB
Function CurrencyStringToNumber(ByVal amount As String) As Double
  If IsNumeric(amount) Then
    Dim actualAmout As String = ""
    For Each c As Char In amount.ToCharArray
      If IsNumeric(c) Or c = "." Then
        actualAmout &= c
      ElseIf c = "(" Then
        actualAmout &= "-"
      End If
    Next
    Return Val(actualAmout)
  Else
    Throw New Exception("Amount is not in proper format.")
  End If
End Function


Craig, please mark it as answer, if it works.
 
Share this answer
 
v3
Comments
[no name] 23-Sep-10 7:24am    
Horribly inefficient

Regex.Replace(input, "[$-.]", "")
Prerak Patel 23-Sep-10 8:24am    
I think you are getting it wrong. Try your regex for ($10,000.50)
t37 23-Sep-10 12:08pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900