Dollar next to number in Tostring format

Asked

Viewed 857 times

4

When I try to format a value using .ToString("c2") do . NET the symbol of the coin comes stuck with the number. This makes the formatting ugly and wrong to my point of view.

Example:

cdec("1234.56").toString("c2") | Resultando: "R$1.234,56"

I would like to return: "R$ 1.234,56" (Separate dollar number).

3 answers

2


An alternative for you would be to take the currency symbol and concatenate with the value using decimal formatting N2.

Would look like this:

Decimal value = 1106.20m;
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine($"Currency Symbol: {NumberFormatInfo.CurrentInfo.CurrencySymbol}");
Console.WriteLine($"Currency Value:  {NumberFormatInfo.CurrentInfo.CurrencySymbol} {value:N2}");
  • Thanks Pablo, I ended up doing a Decimal Xtension for this using this your option. Thank you so much!

1

  • If the person who has denied says what is wrong in my answer I can improve it.

  • It was not I who denied it, but I appreciate your reply. Unfortunately I did not find anything official about the spacing of the currency symbol. However, in windows 7 used without problems the function, already in windows 10 happened to join. Anyway... Thanks for the answer.

  • @Eleisonchristian actually has no problem at all, the person who negatively negatively negatived more things, she must be angry at me. I asked to see if it was a case that person made a mistake. Note that the first paragraph is equal to another answer and the second one shows something that one can, and should, do. Although I don’t like it is less gambiarra than the first solution that does not format monetary value. If one day changes the symbol you will have to hunt in the application every place you must change. And I know you have not denied. It’s what I said, there’s no space because it was set like this in Windows.

  • Yes, basically your answer is the same as before. I had already considered this option (which I ended up adopting) but it is unfortunate not to have direct formatting via toString. Thanks again!

  • @Eleisonchristian Which option did you adopt?

  • I ended up making an Extension of the string with the format proposed by the two options presented. For indeed yours and the previous answer are similar, changing only the Sintax.

Show 1 more comment

0

I use the {0:C}

double valor;
valor = double.Parse(txtValor.Text);

MessageBox.Show("O valor é " +String.Formart("{0:C}", valor));

He takes the value of TextBox and displays on MessageBox in the value in the current currency

Thus, 1000 = R$1,000.00

Below is my script to calculate salary readjustment it is not complete but maybe it will help you enter

        double salario, percentual, aumento, novosal,a5,a10,a15,a20;
        salario = double.Parse(txtSalario.Text);
        a5  = salario * 5 / 100;
        a10 = salario * 10 / 100;
        a15 = salario * 15 / 100;
        a20 = salario * 20 / 100;

        if(salario <= 280)
        {
            novosal = salario + a20;
            percentual = 10*20/100;
            aumento = a20;

            MessageBox.Show("O salário é " +String.Format("{0:C}",salario)+ "\n o percentual de aumento é "+percentual+"\n o valor do aumento foi de" +String.Format("{0:C}",aumento));
        }
        else if (salario > 280 && salario < 700)
        {
            novosal = salario + a15;
            percentual = salario * 15 / 100;
            aumento = a15;

            MessageBox.Show("O salário é " +String.Format("{0:C}",salario) + "\n o percentual de aumento é " + percentual + "\n o valor do aumento foi de" +String.Format("{0:C}", aumento));
        }
        else if (salario > 700 && salario <1500)
        {
            novosal = salario + a10;
            percentual = salario * 10 / 100;
            aumento = a10;

            MessageBox.Show("O salário atual é " +String.Format("{0:C}", salario) + "\n O percentual de aumento foi de " +percentual+ "\n O aumento foi de " + String.Format("{0:C}",aumento) +"\n Salário atualizado é de "+String.Format("{0:C}", novosal));
        }
        else if (salario >= 1500)
        {
            novosal = salario + a5;
            percentual = salario * 5 / 100;
            aumento = a5;

            MessageBox.Show("O salário é " +String.Format("{0:C}", salario) + "\n o percentual de aumento é " + percentual + "\n o valor do aumento foi de" +String.Format("{0:C}", aumento));
        }

hope I’ve helped

  • 1

    I suggest you fix the typos just like String.Formart and focus the answer on what matters, not including Textboxes and Messageboxes.

Browser other questions tagged

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