public string s()
{
d开发者_如何学编程ouble price = 123.12
double preVatPrice = (100 / (100 + 20) + price);
return preVatPrice.ToString();
}
in c# this always returns as '0' any ideas why?
This is integer division:
100 / (100 + 20)
That will result in a 0.
Make it into:
100.0 / (100.0 + 20.0)
To ensure all parameters are floating point types.
BTW - the function returns "123.12" when I test it.
Update:
As pointed out in several comments - you should be using decimal
for monetary calculations, though in this case it is unlikely to be a problem.
The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors.
Some of your literals are ints, use (100.0 / (100.0 + 20.0) + price)
to get double precision arithmetic.
double preVatPrice = ((double)100 / (100 + 20) + price);
精彩评论