Formatting a double to two decimal places

前端 未结 7 576
庸人自扰
庸人自扰 2020-11-27 06:25

I have been trying to make the answer this prints out to be to two decimal places. All the math involved has to stay at that format of two decimal places. I have tried a few

7条回答
  •  攒了一身酷
    2020-11-27 07:05

    You can round a double to two decimal places like this:

    double c;
    c = Math.Round(c, 2);
    

    But beware rounding will eventually bite you, so use it with caution.

    Instead use the decimal data type.

提交回复
热议问题