You can use the class NumberFormatInfo
to specify the information contained in the corresponding numerical value. In addition, it is important that you test as many entries as possible to prevent inconsistent values from being stored or generating possible errors or future problems.
I created the function StrCurrencyToDecimal()
to convert the value into cash for the type decimal
, see:
decimal StrCurrencyToDecimal(string str)
{
NumberFormatInfo infoCurrency = new NumberFormatInfo();
infoCurrency.NegativeSign = "-";
infoCurrency.CurrencyDecimalSeparator = ",";
infoCurrency.CurrencyGroupSeparator = ".";
infoCurrency.CurrencySymbol = "R$";
if (decimal.TryParse(str, NumberStyles.Currency, infoCurrency, out var result))
return result;
return -1;
}
The function returns -1
in the case of invalid values, it is for illustration only, however, there are more appropriate ways for this, you can even use error codes. Of a deal in this answer on this subject.
See the full code:
using System.Globalization;
using static System.Console;
public class Program
{
public static void Main()
{
WriteLine("Valor: {0}", StrCurrencyToDecimal("R$3.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("R$0,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("R$-3.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal(""));
WriteLine("Valor: {0}", StrCurrencyToDecimal("R3.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("$3.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("3.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("R$3ad.852,00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("R$3,852.00"));
WriteLine("Valor: {0}", StrCurrencyToDecimal("assR$3,852.00"));
}
static decimal StrCurrencyToDecimal(string str)
{
NumberFormatInfo infoCurrency = new NumberFormatInfo();
infoCurrency.NegativeSign = "-";
infoCurrency.CurrencyDecimalSeparator = ",";
infoCurrency.CurrencyGroupSeparator = ".";
infoCurrency.CurrencySymbol = "R$";
if (decimal.TryParse(str, NumberStyles.Currency, infoCurrency, out var result))
return result;
return -1;
}
}
Exit:
Value: 3852.00
Value: 0.00
Value: -3852.00
Value: -1
Value: -1
Value: -1
Value: -1
Value: -1
Value: -1
Value: -1
See working on .NET Fiddle.
Learn more about the class Numberformatinfo.
I believe that the only way is to make this field string. decimal will save the value with the decimals, but not with the separation of thousands.
– José Francisco
you would have an example of what it would be like to convert the number with the thousand separation?
– Amaral
It is irrelevant to save the point of the thousand and million if when you turn it into a numeral type it will lose this symbol. If the problem is only for presentation it is better to use a monetary mask.
– Gabriel Coletta
Gabriel’s good solution... here are some examples. C# https://answall.com/questions/129549/c-separar-corta-string-por-v%C3%Adrgulas-por%C3%A9m-with-value-Monet%C3%A1rio-incluso-na-st and in Javascript https://answall.com/questions/188190/formatr-moeda-com-mile-separator
– José Francisco
Which database (DBMS) is using?
– gato