include decimal places Value 1500 and leave it as 1500.00 without comma!

Asked

Viewed 381 times

1

I have an integer value of 1500 in the monetary value variable and want to send to another value variable like 1500.00 . How to do in c#. I have tried conversions and nothing happens... In the end, the variable Decimal Value must be filled as 1500.00. I made several forms and nothing happens. I don’t know what to do. Part of the routine is this;

                string valorreceb = ((registro.Cells["Valor"].Value.ToString()));
                valorreceb = valorreceb.ToString().Replace(".", ",");
                if (string.IsNullOrEmpty(registro.Cells["Valor"].Value.ToString()) || valorreceb == "0")
                {

                    String valor2 = "0.00";
                    cepXml.AppendFormat("\t\t<Valor>{0}</Valor>\r\n", valor2);
                }
                else
                {
                    decimal d = Convert.ToDecimal(valorreceb);
                    string Valordecimal = Convert.ToString(d);
                    string sVariavelNova = Valordecimal.Replace(",", ".");

                    cepXml.AppendFormat("\t\t<Valor>{0}</Valor>\r\n", Valordecimal);
                }
  • I don’t really know how to do it in the technology you’re using, which I don’t even know exactly. But I can almost guarantee you that keep doing conversion, Replace() and things like that are the wrong way. You probably have to use Culture

  • as amazing as it sounds! I did it. the following routine: Cultureinfo _ptbr = Cultureinfo.Createspecificculture("en-BR"); d.Tostring("00.00", _ptbr); string test= string.Format(_ptbr, "{0:00,00}", d);

  • 1

    I thought I could do it, I didn’t answer because the question didn’t make the context of what you were doing so clear. I could get it wrong. You can post as an answer.

1 answer

2

You don’t need anything special, the value being in decimal the method ToString(string format, IFormatProvider provider) already does it, you just need to pass one CultureInfo that uses dot as decimal separator, which can be for example the InvariantCulture

decimal valor = 1500;
string formatado = valor.ToString("f2", System.Globalization.CultureInfo.InvariantCulture)

In case the "f2" indicates to the method ToString that we want to format the value using a fixed point with two decimal places, you can see more about the standard number formatting in the documentation

Browser other questions tagged

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