Click here to Skip to main content
Licence 
First Posted 5 Jun 2006
Views 70,370
Bookmarked 12 times

Convert String To Decimal

By Edusie | 5 Jun 2006 | Unedited contribution
How convert a string to a decimal using .NET
   1.48 (17 votes)
12 votes, 70.6%
1
1 vote, 5.9%
2

3
2 votes, 11.8%
4
2 votes, 11.8%
5
1.48/5 - 17 votes
2 removed
μ 1.87, σa 2.69 [?]
 

Introduction

I tried to convert a currency string ("$ 1.200,00") to decimal using c# and VS2005, I can't did it using Decimal.Parse("$ 1.200,00"). 
Allways the error message was "Input String was not in a correct format".  But I saw
I was using the correct format.

Solution

Then I retrieved a old code using NumberStyles. I used the same code but the error still the same.

I did test the line code and when I removed the space from the currency string. Voila, it works.

This is the function.

private decimal ToDecimal(string Value)
{
 if (Value.Length == 0)
                return 0;
        else
                return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.AllowThousands 
   | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Edusie

Web Developer

Colombia Colombia

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank You ! Pinmemberpolczym9:42 8 May '10  
GeneralNice PinmemberRavi J Patel20:15 4 May '10  
GeneralConvert string to decimal , rithy(cambodia) Pinmemberthyrith22:54 30 Mar '10  
GeneralSolution Pinmemberrosilvioro3:48 21 Jul '09  
JokeExcelent Article PinmemberJuan Carlos Chávez Trinidad17:34 24 Jun '09  
GeneralMy vote of 1 Pinmemberlousyprogrammer19:05 4 Jun '09  
GeneralMy vote of 1 Pinmembermuz@dogru.ch1:49 19 Dec '08  
General',' vs '.' PinmemberEuGenius_Krivbass6:53 24 Apr '08  
GeneralRe: ',' vs '.' Pinmemberbeimklabautermann6:39 3 Jun '10  
GeneralThank You PinmemberNachiappanR22:40 10 Jul '07  
Generaluse decimal.Parse instead Pinmembertekun15:12 6 Nov '06  
GeneralCultureInfo PinmemberErnst Naezer0:52 6 Jun '06  
QuestionWhy so complicated? Pinmembermav.northwind23:07 5 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120217.1 | Last Updated 6 Jun 2006
Article Copyright 2006 by Edusie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid