Trunk a double ?


I want to know how I can trunk a double ?

I do a average function and the result is : X.xx km/h and I want jsut X.x km/h, is it possible ?

Thanks a lot

Serial.print(floatNmumber, 1);

will print the number with one decimal place, if that is what you are asking.

If you are asking to modify the actual number (rather than how it's displayed as groundfungus showed you), try something like:

void setup() {


  float kph = 12.34;
  int temp;
  temp = (int) (kph * 10.0);     // This makes temp = 123
  kph = ((float) temp / 10.0);   // Now it's 12.3

void loop() {

This changes the actual value of kph to 12.3.

Something is weird...

Your answers is working, i don't have 12.34 but 12.30

the fourth number is still here...

the fourth number is still here...

Are you seeing this in Serial.print() ? Unless you specify otherwise it will print 2 decimal places of a float.

A question for you. Do you want to truncate it or round it ?

That's where you use groundfungus' Serial.print(). You need to frame your question more clearly as to where you want the number correct or displayed with just one decimal digit.

Do you want to truncate it or round it ?

OP clearly stated that he/she wanted to trunk it. Can't you read? 8)

Whatever the hell "trunk it" means. I think of dead body disposal, but that probably isn't what OP had in mind.

I want to trunk the number and display it on an OLED I2C screen, not on the serial monitor..

Whatever it means you can apparently conjugate the verb form of it English verb trunk conjugated in all tenses

I want to trunk the number and display it on an OLED I2C screen, not on the serial monitor..

Well we still don't know what the hell "trunk" means to you. It means a large box with handles on both ends and a latch on the front and hinges on the back to me. It also means the opening at the back of the car, but some people are weird and use other names for that space.

Truncate means something specific. As to how you would truncate the number to display on the LCD, you wouldn't. You would truncate the string created from the number. As to how to do that, I suggest that you fix line number 556757151812427184274818154 in your code.

Oh sorry I don't understand...

I display the speed average on my oled i2c screen. For the moment it's displaying like 1,32km/h but I just want to display 1.3km/h...

Where did you learn the word trunk ?

The code used to display the number is what ?

What would you like it to display if the number it displays now is 1,39 ?

You will have to distinguish between how a number is stored and how it is presented.

double x = 12.34;
char buf[20];

void setup(){
  Serial.println(x);//print value
  x = floor(x*10)/10;//truncate value to 1 decimal
  Serial.println(x);//print value
  dtostrf(x, 5, 1, buf);//format prinout string with 1 decimal
  Serial.println(buf);//print formatted string

void loop(){}//loop()

YEs it’s works !

Thanks a lot ! <3