float number only with two digit after point

1 - How could store float number only with two digit after point in a variable ?

for example

13.436565

need this only 13.43

2- what is he difference between float and double ?

On arduino float is the same as a double.

Your float will be of 6-7 decimal places of precision. If you want only two, you could multiply your float by 100 and cast it into an int or a long integer type.

void setup ()
  {
  Serial.begin (115200);

  float foo = 13.436565;
  Serial.println (foo, 2);
  }  // end of setup

void loop () { }

Output:

13.44

[quote author=Nick Gammon link=topic=269306.msg1898486#msg1898486 date=1411802522]

void setup ()
  {
  Serial.begin (115200);

  float foo = 13.436565;
  Serial.println (foo, 2);
  }  // end of setup

void loop () { }

Output:

13.44

[/quote]Or you can use Nick's example and pretend the trailing digits are not there :blush:

And you don't have to even trouble yourself... serial.println() will print two decimal places by default:

Serial.println(foo);

RoboTemad:
1 - How could store float number only with two digit after point in a variable ?

What do you mean by store?
On an SD card? in a text file?
in EEPROM?
in a variable?

BulldogLowell:
On arduino float is the same as a double.

Your float will be of 6-7 decimal places of precision. If you want only two, you could multiply your float by 100 and cast it into an int or a long integer type.

thanks for reply

you meant like this

 float r =13.436565;
 float y;
 int x;
 x = (int (13.436565*100))  ;
 y = float (x)/ 100;

example of Nick , it is just for printing not for store in a variable .

1 Like