Convert number c#

Asked

Viewed 284 times

0

How to convert R$3,852.00 and save in these bank . and ,? Follow my code.

 public ImportProcesso()
        {
            ImportProcesso processo = new ImportProcesso();
            processo.dataReqPgto = new System.DateTime();
            processo.datRecebOrigem = new System.DateTime();
            processo.datSEP = new System.DateTime();
            processo.dscComarca = "";
            processo.dscmunicipio = "";
            processo.endereco = "";
            processo.nmebairro = "";
            processo.nmeInteressado = "";
            processo.nmeVara ="";
            processo.numero = "";
            processo.numProcAnterior = "";
            processo.numProcJudicial = "";
            processo.numVara = 0;
            processo.vlrbruto = System.Decimal ;

I accept the suggestion if I have to supplement more information. this structure is correct of the method?

  • 1

    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.

  • you would have an example of what it would be like to convert the number with the thousand separation?

  • 1

    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’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

  • Which database (DBMS) is using?

2 answers

1


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.

0

Use the following method

    public static decimal Sem_Simbolo(string pValor_Reais)
    {
        return Convert.ToDecimal(pValor_Reais.Replace("R$", string.Empty));
    }

Or convert straight, whatever. But it will save as decimal, as the bank is asking, IE, the saved value will be 3852,00, without the point.

  • in the database as Number(15,2), I am trying to convert this field Importprocess = new Importprocess(); process.dataReqPgto = new System.Datetime();process.vlrbruto = ; but I tried several ways and could not

  • use the method that will work

  • Yes friend, I tried and saw that it works, my doubt is to apply on this line of the process code.numProcAnterior = ""; process.numProcJuditial = ""; process.numVara = 0; process.vlrbruto =

  • Solved, I did a direct process.vlrbruto = Convert.Todecimal(vlrbruto.Tostring()); Thanks a friend

  • If solved can mark as answer :)

  • 1

    Of course, brother Marceloawq, thank you very much and for the good tips, you contributed a lot and to my doubts.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.