Doing Math.Round (x, 2) on a decimal value, but you need to provide 2 numbers after the decimal as money

Some values ​​return 2.0, but I need it to be 2.00, as this is the monetary value displayed on the web page.

I do:

Math.Round(value, 2);

Is there a way to force it to 2 numbers after the decimal number?

+3
source share
3 answers

You must use a decimal to store monetary values.

But regardless of whether you are using a decimal or floating point type, your question asks how a number can be displayed with two decimal places. Use money.ToString("0.00")to display two decimal places.

+5

VB.Net,

FormatNumber(number, digitsAfter)

FormatCurrency(number, digitsAfter)

(#, Vb.Net)

doubleNumber = -1898300.1987;
Console.WriteLine(doubleNumber.ToString("F2", CultureInfo.InvariantCulture));  
// Displays -1898300.20

ToString(): http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

+2

Either .ToString ("0.00"), as suggested by Mark, if you need to display only .00 or Math.Round (value * 100.0) /100.0; if you need the actual value with percentage accuracy.

0
source

Source: https://habr.com/ru/post/1735343/


All Articles