Description

Draw a float number using specified font number or current font.

Use with setTextDatum() to position values on TFT, and setTextPadding() to blank old displayed values.

Signature

  • int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y, uint8_t font)
  • int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y)

Parameters

  • float floatNumber: Float to be drawn.
  • uint8t decimal: number of decimal places to render.
  • int32_t x: x coord.
  • int32_t y: y coord.
  • uint8_t font: Font number.

Result

uint16_t: the width of the float representation in pixels.

Example

#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

void setup() {
  Serial.begin(250000);
  tft.begin();
  tft.setRotation(1); // Set the rotation of the display
}

void loop() {
  tft.fillScreen(TFT_BLACK); // Clear the screen with black color
  tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the text color to white with black background
  tft.loadFont(AA_FONT_SMALL); // Load a small anti-aliased font

  // Draw a float at position (100, 100) on the screen
  tft.drawFloat(1024.0 / 7.0, 10, 100, 100);

  delay(1000); // Wait for 1 second
}

In this example, we first initialize the TFT display and set its rotation. Then, in the loop() function, we clear the screen with black color, set the text color to white with black background, and load a small anti-aliased font. Finally, we use drawFloat() to draw the float 1024/7 "146.2857143" at position (100, 100) on the screen.

Also, you can use setTextDatum() function to set the datum (reference point) for the text, which can be useful for aligning text to the left, right, or center of the screen. For example:

tft.setTextDatum(MR_DATUM); // Set the datum to the middle right of the text
tft.drawFloat(1024.0 / 7.0, 10, 100, 100);

This would draw the float with its middle right corner at position (100, 100) on the screen.