multiplication of double with integer precision

后端 未结 5 897
难免孤独
难免孤独 2021-01-22 20:35

I have a double of 3.4. However, when I multiply it with 100, it gives 339 instead of 340. It seems to be caused by the precision of double. How could I get around this?

5条回答
  •  北海茫月
    2021-01-22 21:32

    You could use two integers and multiply the fractional part by multiplier / 10.

    E.g

    int d[2] = {3,4};
    int n = (d[0] * 100) + (d[1] * 10);
    

    If you really want all that precision either side of the decimal point. Really does depend on the application.

提交回复
热议问题