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"))
.– Maniero
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"));
– Matheus Muniz
The first appears End of Expression expected
– Matheus Muniz
Fix: string Test = "9.5744"; string result = "Cost: R$"+ string. Format("{0:C2}",Test). Tostring();
– Matheus Muniz
You’re probably typing something wrong and in the other case you’re not inserting the
namespace
of the class that is theGlobalization
.– Maniero