简体   繁体   中英

Unwanted Roundings with Math.Pow

I'm doing some simple calculations and the value entered in a calculator gives me 1.000243872 but it gives me 1. I require the decimal Points. Was wondering what may be the issue for the rounding?

Private Decimal APR = 4.5, Private Decimal PF = 2, Private Decimal CF = 2

var value = (double) (1 + (double) APR/(double) PF); => 1.04550625
var value1 = (double)Math.Pow((double)value, (double) CF); =>  1.093083319
var value2 = (double) Math.Pow((double) value1, (double)(1/365)); => 1

You're dividing two int values : 1 and 365 . In most programming languages, this division returns an int value, which in your particular case is 0. The (double) cast is performed only after the result has been computed. (double)0 is 0 . To get a fractionary result, you could either use a cast on one of the operands : (double)1/365 or 1/(double)365 , or use what others have suggested : 1.0/365 or 1/365.0 .

1/365 is 0 (it's almost 0, but then becomes 0 because you are dealing with integers inputs therefore you only get the integer result), then casted to a double it's still 0 and anything ^0 = 1

you could try 1.0 / 365

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