C# calculate decimal -
i want calculate price doing simple sum. example:
float hourpay = 18.30 int minutesworked = 125 minutesworked \ 60 * hourpay = 38,124999999 i got these values in c# function , running 2 problems:
private decimal calculateprice(int minutes) { float test = minutes / 60 * setting.hourpay return; } the problems are, test returns 36.6 not correct , rounded off 1 decimal.
i want correct answer parses decimal , rounds 2 decimals.
the problem starting dividing 2 integers, results in integer:
float test = minutes / 60; note test get's 2, because decimals cut off if 2 integers divided. calculation right:
2 * 18.3 = 36.6
test should 2.083.... solution cast integers floats first:
private decimal calculateprice(int minutes) { float test = (float)minutes / 60.0f * setting.hourpay; return; } then division not ignore decimals , result exprected. can leave out explicit cast of minutes:
float test = minutes / 60.0f * setting.hourpay; this valid long constant (60.0f) marked float, because integers divided floats implictly casted integers. however, first approach easier read.
Comments
Post a Comment