简体   繁体   中英

C# : How to round up float value to nearest float value?

I have a datagridview column 'total',in which I am doing sum of rates of respective items entered.Sometimes I am getting addition like 30.19 Rupees or 100.59 rupees,I want to make 30.19 to 30.50 & 100.59 to 101.So can anyone plz help me..?

i have tried

                float val = float.Parse(value);
                total = val;

                ekunrakam = ekunrakam + total;
                Math.Round(ekunrakam + 0.5,2);

but its not working..

thanks in advance..

Usually with currency, in the cases where you don't have a dedicated currency type, you use the smallest unit of currency that exists, paise or cents or what have you.

Also, don't use floats for money, or the rounding errors will get you. These folk suggest Decimal.

What is the best data type to use for money in c#?

Most countries have an official standard for how to round fractional amounts of currency, sometimes required by law, but they usually are some variant of bankers rounding. Which as luck will have it is the default behaviour of Decimal.Round.

http://msdn.microsoft.com/en-us/library/system.decimal.round%28v=vs.71%29.aspx

您希望Math.Round(Somefloat + 0.5,2)可以完成这项工作。

If you were wanting to round to the nearest whole number, like 100.59 to 101, I would tell you to just set your float variable to an int. But, why do you want to round up to 30.50? What are your specific rules for rounding?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM