C/C++ counting the number of decimals?

后端 未结 13 1955
北荒
北荒 2020-12-06 16:04

Lets say that input from the user is a decimal number, ex. 5.2155 (having 4 decimal digits). It can be stored freely (int,double) etc.

Is there any

13条回答
  •  春和景丽
    2020-12-06 16:55

    Off the top of my head:

    start with the fractional portion: .2155

    repeatedly multiply by 10 and throw away the integer portion of the number until you get zero. The number of steps will be the number of decimals. e.g:

    .2155 * 10 = 2.155
    .155 * 10 = 1.55
    .55 * 10 = 5.5
    .5 * 10 = 5.0
    

    4 steps = 4 decimal digits

提交回复
热议问题