Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding Inside razor MVC

I am doing some arithmetic calculation inside Razor view as below

decimal n = Convert.ToDecimal(Model.np_claim_dtls[i].PROV_PRICE);
decimal d = Convert.ToDecimal(Model.np_claim_dtls[i].DISCOUNT_AMOUNT);
<text> @(n-d) </text>

its giving me result as below

923.0340000000000

expected result is

 923.03

How can i use rounding inside razor? Please help

like image 792
Sachu Avatar asked Dec 04 '25 11:12

Sachu


2 Answers

You can round off the values like

decimal d = 100.1255M; 

string s1 = d.ToString( "#.##" ); // 100.13
string s2 = d.ToString( "0.00" ); // 100.13

This can also be done in the controller rather than view. Exposing computations in view is not recommended

like image 151
Tushar Avatar answered Dec 07 '25 15:12

Tushar


This should be working :

decimal n = Convert.ToDecimal(Model.np_claim_dtls[i].PROV_PRICE);
decimal d = Convert.ToDecimal(Model.np_claim_dtls[i].DISCOUNT_AMOUNT);

<text> @(Math.Round(n - d, 2)) </text>
like image 42
Ben Avatar answered Dec 07 '25 13:12

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!