# How to change integer to float?

**URL:** <https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924>\
**Category:** Programming\
**Created:** [June 18, 2026, 2:47pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924 "2026-06-18T14:47:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marty\_k](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/marty_k/32/1275700_2.png) [@marty\_k](https://forum.arduino.cc/u/marty_k)\
**Post date:** [June 18, 2026, 2:47pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/1 "2026-06-18T14:47:06Z")

</div>

I have a SparkFun RedBoard (Uno) that reads 4 analog inputs and scales them to output to 4 Sparkfun Qwiic HT16K33 displays.

The Arduino sketch (from Github) is displaying integers only.  
I need channel A0 in the format: -40.0 to +40.0

Thanks. Marty

```arduino
#include <Wire.h>
#include <SparkFun_Alphanumeric_Display.h>

// I²C addresses for Qwiic Alphanumeric Displays
const byte displayAddresses[4] = {0x70, 0x71, 0x72, 0x73};

// Create display objects for each address
HT16K33 displays[4];

// Analog pins for each display
const byte analogPins[4] = {A0, A1, A2, A3};

// Scaling parameters for each display
struct ScaleParams
{
  float inMin; // Minimum input voltage
  float inMax; // Maximum input voltage
  float outMin; // Minimum output value
  float outMax; // Maximum output value
  char format[8]; // Format for display
};

// Scaling values for each display
ScaleParams scale[4] =
  // A0 temp ext 0.1 ─► 0.9v displays -40.0 -> +40.0
  // A1 humid ext 0.7 ─► 2.3v displays 10 -> 80
  // A2 humid int 0.7 ─► 2.3v displays 10 -> 80
  // A3 BAP 1.7 ─► 2.0v displays 900 -> 1100
{
  // Display A0
  {0.1, 0.9, -40.0, +40.0, "%+3d"},

  // Display A1
  {0.7, 2.3, 10, 80, "%3d"},

  // Display A2
  {0.7, 2.3, 10, 80, "%3d"},

  // Display A3
  {1.5, 2.0, 900, 1100, "%3d"}
};

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

// Initialize each display
for (int i = 0; i < 4; i++)
{
 displays[i].begin(displayAddresses[i]);
 displays[i].setBrightness(0);
 displays[i].clear();
 displays[i].print(i+1);
 delay(150);
}
}

float analogToVoltage(float analogVal)
{
  return analogVal * (3.3 / 1023);
}

void loop()
{
  for (int i = 0; i < 4; i++)
  {
  float analogVal = analogRead(analogPins[i]);
  float voltage = analogToVoltage(analogVal);

  // Map voltage to scaled output
  float scaled = (voltage - scale[i].inMin) * (scale[i].outMax -
  scale[i].outMin) / (scale[i].inMax - scale[i].inMin) + scale[i].outMin;

  // Clamp output to min/max
  //if (scaled < scale[i].outMin) scaled = scale[i].outMin;
  //if (scaled > scale[i].outMax) scaled = scale[i].outMax;

  char buffer[8];
  snprintf(buffer, sizeof(buffer), scale[i].format, (int)scaled);

  displays[i].print(buffer);
}

delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [June 18, 2026, 3:14pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/2 "2026-06-18T15:14:25Z")

</div>

> [@marty\_k](#):
>
> `(int)scaled);`

Because of this. You convert it to integer.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [June 18, 2026, 3:59pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/4 "2026-06-18T15:59:12Z")

</div>

Check my HT16K33 library, it has several dedicated print functions including:

- **bool displayFloat(float f, uint8\_t decimals = 3)**
- **bool displayFixedPoint1(float f)** displays values -99.9 .. 999.9 with 1 decimal.

> **[GitHub - RobTillaart/HT16K33: Arduino Library for HT16K33 4x7segment display](https://github.com/RobTillaart/HT16K33)**
>
> Arduino Library for HT16K33 4x7segment display

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 18, 2026, 4:03pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/5 "2026-06-18T16:03:39Z")

</div>

> [@marty\_k](#):
>
> `snprintf(buffer, sizeof(buffer), scale[i].format, (int)scaled);`

See this thread about sprintf() with float values.

[https://forum.arduino.cc/t/sprintf-with-float-values/937562](https://forum.arduino.cc/t/sprintf-with-float-values/937562)

It's not supported on the Uno and other 8 bit avr boards. Use dtostrf().

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 18, 2026, 9:05pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/6 "2026-06-18T21:05:47Z")

</div>

> [@marty\_k](#):
>
> I need channel A0 in the format: -40.0 to +40.0

AnalogRead returns integers, so try this approach.

```arduino
int rawValue = analogRead (A0);
float value = map (rawValue, 0, 1023, -400, 400) / 10;

```

or, shorter,

`float value = map (analogRead(A0), 0, 1023, -400, 400) / 10;`

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 19, 2026, 8:26am UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/7 "2026-06-19T08:26:30Z")

</div>

**1.** UNO R3 is a unipolar ADC which means it cannot convert -ve DC voltage into biary bits.

**2.** Let us see how to convert the signal coming from ADC-Ch0 (A0) into floating form. Connect 3.3V pin with A0-pin by a jumper wire.

```arduino
unsigned int y = analogRead(A0); //y ~= 675
float voltY = (5.0/1023.0) * y; // voltY ~= 3.3
Serial.println(voltY, 1); //shows: approx. 3.3V

```

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 19, 2026, 10:33am UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/8 "2026-06-19T10:33:42Z")

</div>

Let's not forget that the Uno R3 doesn't have an A/D that can reliably read voltages.  
It's ratiometric A/D relies on the potentially unstanle USB supply voltage.

It is not clear to me what OP's project is or does.

---

<div class="post-metadata">

**Author:** ![marty\_k](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/marty_k/32/1275700_2.png) [@marty\_k](https://forum.arduino.cc/u/marty_k)\
**Post date:** [June 19, 2026, 11:48am UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/9 "2026-06-19T11:48:55Z")

</div>

Golam

If you red carefully (past tense of READ) , you would see there is no negative voltages.

0.1 -\> 0.9v to display -40.0 ─► +40.0

---

<div class="post-metadata">

**Author:** ![marty\_k](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/marty_k/32/1275700_2.png) [@marty\_k](https://forum.arduino.cc/u/marty_k)\
**Post date:** [June 19, 2026, 12:34pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/10 "2026-06-19T12:34:47Z")

</div>

Thanks for this but my displays are 14-segment, not 7.

---

<div class="post-metadata">

**Author:** ![marty\_k](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/marty_k/32/1275700_2.png) [@marty\_k](https://forum.arduino.cc/u/marty_k)\
**Post date:** [June 19, 2026, 12:50pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/11 "2026-06-19T12:50:15Z")

</div>

Who is OP?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 19, 2026, 1:41pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/12 "2026-06-19T13:41:53Z")

</div>

As I undersrand OP, TO, and TP are synonyms.

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [June 19, 2026, 1:44pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/13 "2026-06-19T13:44:24Z")

</div>

![Screenshot 2026-06-19 at 8.43.45 AM](https://europe1.discourse-cdn.com/arduino/original/4X/4/1/1/411dc75a53ade2d6cbafcad788e79022362e76a3.png)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [June 19, 2026, 1:47pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/14 "2026-06-19T13:47:00Z")

</div>

> [@Wawa](#):
>
> float value = map (analogRead(A0), 0, 1023, -400, 400) / 10;

You get an integral if you do that - you won’t have one decimal. You need to divide by 10.0 to get the division to be performed as a float.

> [@marty\_k](#):
>
> past tense of READ

Preterite is spelled read (but indeed pronounced “red”)

---

<div class="post-metadata">

**Author:** ![mikedb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikedb/32/1098613_2.png) [@mikedb](https://forum.arduino.cc/u/mikedb)\
**Post date:** [June 19, 2026, 1:56pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/15 "2026-06-19T13:56:12Z")

</div>

How did you do that??

 ![Screenshot 2026-06-19 at 8.43.45 AM](https://europe1.discourse-cdn.com/arduino/original/4X/4/1/1/411dc75a53ade2d6cbafcad788e79022362e76a3.png)

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [June 19, 2026, 1:58pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/16 "2026-06-19T13:58:01Z")

</div>

I hovered over the OP's avatar which produced the text then took a screen shot of the area of interest.

---

<div class="post-metadata">

**Author:** ![mikedb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikedb/32/1098613_2.png) [@mikedb](https://forum.arduino.cc/u/mikedb)\
**Post date:** [June 19, 2026, 2:00pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/17 "2026-06-19T14:00:39Z")

</div>

Thanks , does not work for me.

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [June 19, 2026, 2:01pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/18 "2026-06-19T14:01:08Z")

</div>

Must be a Mac Safari thing.  
Handy for discovering other things:

 ![Screenshot 2026-06-19 at 9.03.26 AM](https://europe1.discourse-cdn.com/arduino/original/4X/8/8/2/8822c041fbdf7238a562b3d84806f2dc7e1c25a6.png)  
 ![Screenshot 2026-06-19 at 9.03.17 AM](https://europe1.discourse-cdn.com/arduino/original/4X/a/f/b/afb4b6c3eedc8a663e07ced49abb94f430d6ef3e.png)

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [June 19, 2026, 3:04pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/19 "2026-06-19T15:04:18Z")

</div>

You can just leave the number as an integer, expressed as tenths, and go through a rather convoluted process to print that as a decimal number.

```arduino
void setup() {
  Serial.begin(115200);
  int valueTenths = map (analogRead(A0), 0, 1023, -400, 400);
  char buffer[8];
  snprintf(buffer, sizeof(buffer), "%c%2d.%1d", (valueTenths < 0) ? '-' : '+', abs(valueTenths) / 10, abs(valueTenths) % 10);
  Serial.println(buffer);
}

void loop() {
}
```

It gets a little more complicated if you want the +/- to immediately proceed the number instead of being in a fixed position, but dtostrf() presents a similar problem where it does not show the '+' character for positive numbers.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 19, 2026, 8:11pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/20 "2026-06-19T20:11:47Z")

</div>

> [@marty\_k](#):
>
> 0.1 -\> 0.9v to display -40.0 -\> +40.0

You can only get that resolution by first setting the A/D reference to 1.1volt.

```arduino
void setup() {
    analogReference(INTERNAL); // set Aref to 1V1
}

```

Then you must find the returned A/D values at 0V1 and 0V9 for your board and use them in the map() command.

Example:  
if 0.1volt returns 110 and 0.9volt returns 920...

```arduino
int rawValue = analogRead (A0);
Serial.println (rawValue); // see what values you get
float value = map (rawValue, 110, 920, -400, 400) / 10.0; // convert to -40/+40 float

```

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 19, 2026, 8:22pm UTC](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924/21 "2026-06-19T20:22:32Z")

</div>

> [@J-M-L](#):
>
> You need to divide by 10.0 to get the division to be performed as a float.

Correct.  
Posted before the morning coffee.  
Thanks for correcting me.

[Next page](https://forum.arduino.cc/t/how-to-change-integer-to-float/1448924.md?page=2)
