How to 'cout' the correct number of decimal places of a double value?

前端 未结 9 1844
迷失自我
迷失自我 2020-11-27 07:44

I need help on keeping the precision of a double. If I assign a literal to a double, the actual value was truncated.

int main() {
    double x =         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 08:10

    Responding to your answer-edit: There is no way to do that. As soon as you assign a value to a double, any trailing zeroes are lost (to the compiler/computer, 0.402, 0.4020, and 0.40200 are the SAME NUMBER). The only way to retain trailing zeroes as you indicated is to store the values as strings (or do trickery where you keep track of the number of digits you care about and format it to exactly that length).

提交回复
热议问题