Formatting values per Razor pages

Asked

Viewed 174 times

2

I’m having problems formatting a variable through the Razor pages in C#.

Let’s say that a variable called value receives the number 9.5723 (real nine and 57 cents), I need all decimals above 2 are discarded and the variable becomes 9.57 only.

I tried using the following syntax

string valor = "9.5700"    
string valor = "Custo: R$" + string.Format("{0:F2}", valor).Replace(".", ",");

But this string.Format() did not work. I also tried to use Split() hoping to work but not a valid method. How could I solve it? Format() is valid?

  • Tries $"Custo: R${valor:C2}" or $"Custo: R${valor:C2}}".ToString(new CultureInfo("pt-BR")).

  • Using the second option I’m getting the return "The type or namespace name 'Cultureinfo' is not Valid in this Scope". I used the following code to test, do not know where I missed string Test = "9.5744"; "9.5744" var result = "Cost: R${test:C2}}". Tostring(new Cultureinfo("en-BR"));

  • The first appears End of Expression expected

  • Fix: string Test = "9.5744"; string result = "Cost: R$"+ string. Format("{0:C2}",Test). Tostring();

  • You’re probably typing something wrong and in the other case you’re not inserting the namespace of the class that is the Globalization.

2 answers

5

Do so:

using static System.Console;
using System.Globalization;

public class Program {
    public static void Main() {
        var valor = "9.5789";
        WriteLine($"Custo: R${valor:C2}");
        WriteLine($"Custo: R${valor:C2}".ToString(new CultureInfo("pt-BR")));
        WriteLine(string.Format("Custo: R${0:C2}", valor, new CultureInfo("pt-BR")));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I just think it’s weird to format a value that’s already like string, probably this is wrong too.

The second is only necessary if the culture of the system is not what you need, it depends on what you want, whether it is to respect the culture of the system or ensure that it is the Brazilian. The second is not so efficient so I think it pays to use the third in this case.

I’m using the monetary value formatter instead of the scientific value formatter and I’m avoiding going back and forth to change points that are even inefficient, and potentially wrong.

  • po, the point is that this is the solution for the Controller, I need a solution in the pages of Razor, because I can not control the information that comes from the API inside it.

  • 2

    Now put it where you need it. If you can’t do it, give it up. Not least because the question has nothing to do with Razor, it just says it wants it, but it’s not our goal to do it for you, we help in solving the problem according to what was asked, to do for you the Razor code that you didn’t do (at least didn’t post) is not what we do here, this is wrong.

1


To format Real values on Razor do it this way buddy:

@{ 
  var dblNumber = 9.5700;
}

@Html.Raw(dblNumber.ToString("C2"))

Exit:

R$ 9,57

Note: If your value is actually stored in a string, for this above method to work you have to replace the "." by "," and convert to double.

More information: Standard Numeric Format Strings

Browser other questions tagged

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