 # how does can be convert into two decimal places [Answered]RSS

## 2 replies

Last post Jan 29, 2019 06:07 PM by kashyapa

• ### how does can be convert into two decimal places

Jan 29, 2019 09:01 AM|polachan|LINK

I have the following code an I want to convert it into two decimal places like  Please help

SalaryPerHour = 20000.0000

SalaryPerMonth= 1000.0000

I want to convert it as 20000.00 and 1000.00

HourlySalary = Convert.ToDecimal(row["SalaryPerHour"].ToString()),
MonthlySalary = Convert.ToDecimal(row["SalaryPerMonth"].ToString()),
YearlySalary = Convert.ToDecimal(row["SalaryPerYear"].ToString()),

• ### Re: how does can be convert into two decimal places

Jan 29, 2019 11:41 AM|polachan|LINK

I corrected  using Math.Round

HourlySalary = Math.Round(Convert.ToDecimal(row["SalaryPerHour"].ToString()),2)

Thanks

• ### Re: how does can be convert into two decimal places

Jan 29, 2019 06:07 PM|kashyapa|LINK

Hi

You dont need Math.Round() for what you are trying to do. C# has various numeric formatters. And the one you need is called as Fixed Point format.

Please go through the documentation here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-fixed-point-f-format-specifier

In Short - when you do ToString() use the format "F2" … that will format the number to 2 decimal points.

Hope this helps.

Lohith G N
http://about.me/kashyapa