Click here to Skip to main content
12,401,867 members (59,974 online)
Click here to Skip to main content
Add your own
alternative version

Stats

104.6K views
12 bookmarked
Posted

Convert String To Decimal

, 5 Jun 2006
Rate this:
Please Sign up or sign in to vote.
How convert a string to a decimal using .NET
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

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

Share

About the Author

Edusie
Web Developer
Colombia Colombia
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThank You ! Pin
polczym8-May-10 8:42
memberpolczym8-May-10 8:42 
GeneralNice Pin
Ravi J Patel4-May-10 19:15
memberRavi J Patel4-May-10 19:15 
GeneralConvert string to decimal , rithy(cambodia) Pin
thyrith30-Mar-10 21:54
memberthyrith30-Mar-10 21:54 
GeneralSolution Pin
rosilvioro21-Jul-09 2:48
memberrosilvioro21-Jul-09 2:48 
JokeExcelent Article Pin
Juan Carlos Chávez Trinidad24-Jun-09 16:34
memberJuan Carlos Chávez Trinidad24-Jun-09 16:34 
GeneralMy vote of 1 Pin
lousyprogrammer4-Jun-09 18:05
memberlousyprogrammer4-Jun-09 18:05 
GeneralMy vote of 1 Pin
muz@dogru.ch19-Dec-08 0:49
membermuz@dogru.ch19-Dec-08 0:49 
General',' vs '.' Pin
EuGenius_Krivbass24-Apr-08 5:53
memberEuGenius_Krivbass24-Apr-08 5:53 
GeneralRe: ',' vs '.' Pin
beimklabautermann3-Jun-10 5:39
memberbeimklabautermann3-Jun-10 5:39 
GeneralThank You Pin
NachiappanR10-Jul-07 21:40
memberNachiappanR10-Jul-07 21:40 
Generaluse decimal.Parse instead Pin
tekun6-Nov-06 14:12
membertekun6-Nov-06 14:12 
GeneralCultureInfo Pin
Ernst Naezer5-Jun-06 23:52
memberErnst Naezer5-Jun-06 23:52 
QuestionWhy so complicated? Pin
mav.northwind5-Jun-06 22:07
membermav.northwind5-Jun-06 22:07 

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

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

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