This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using float variable

Hello everyone !

I have a problem with the float variables.

For example, i define float a = 123456123456.1234 and char lcd[30];

I used sprintf function to convert a to string and store into lcd (sprintf(lcd,"%0.2f",a);).

Then, i export lcd to LCD 16x2.

Result i see on the LCD is "123456100000.00"

Why this ?

And can everyone show me how to print a on LCD ("123456123456.1234")?

Best regard !

Parents
  • Please take a closer look at the float data type.

    If you have a signed 4-byte integer, the largest number you can store is about 2.000.000.000, so about 9 value digits.

    If you instead use the 4 bytes for a floating point value, where some of the room is needed for mantissa and some for exponent, then you aren't likely to be able to store even more value digits than if you fit an integer in that same storage space...

    If you want to play with lots of value digits, then you need to play with some form of fixed-point arithmetic - and implement or use a big-number library where the value is stored in an array of bytes or integers.

Reply
  • Please take a closer look at the float data type.

    If you have a signed 4-byte integer, the largest number you can store is about 2.000.000.000, so about 9 value digits.

    If you instead use the 4 bytes for a floating point value, where some of the room is needed for mantissa and some for exponent, then you aren't likely to be able to store even more value digits than if you fit an integer in that same storage space...

    If you want to play with lots of value digits, then you need to play with some form of fixed-point arithmetic - and implement or use a big-number library where the value is stored in an array of bytes or integers.

Children