How to store a number in decimal format in avr

前端 未结 1 1356
予麋鹿
予麋鹿 2020-12-22 05:20

I am trying to enter the decimal values using a keypad in ATMega8 Till now I have been able to enter only the integer values The code is given below

switch (         


        
相关标签:
1条回答
  • 2020-12-22 06:01

    Its basic high school math, you need represent numbers in powers of 10.

    Example - 
    138.25 = (1 * 10^2) + (3 * 10^1) + (8 * 10^0) +         // integer part
             (2 * 10^-1) + (5 * 10^-2)                      // Float part
    

    I will not give you complete code, but you can use this idea

    if (decimal)
    {
        a = a + b / (10 ^ pow);
    }
    else
    {
        a = a * 10 + b
    }
    

    pow is the decimal digit - In above example (138.25) pow 2 is 1, and pow for 5 is 2. So you need to maintain counter for pow

    0 讨论(0)
提交回复
热议问题