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

1

I have an integer value of 1500 in the variable valuemonetary and I want to send it to another valuevalue variable like 1500.00. How to do in C #. I've tried conversions and nothing happens ... In the end, the variable ValueDecimal should be filled as 1500.00. I made many forms and nothing happens. I do not 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);
                }
    
asked by anonymous 21.09.2016 / 20:31

1 answer

2

You do not need anything special, the value being decimal method ToString(string format, IFormatProvider provider) already does this, you only need to pass CultureInfo that uses dot as decimal separator, which can be for example InvariantCulture

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

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

    
21.09.2016 / 22:28