How to get string from float

Hi,

I am trying to send wirelessly data to another Arduino, but I cannot do that because it is float data type.
Now, I have investigated that I can float multiply and get full number and then convert to string. But I do not know how to multiply! :frowning:

Serial.println(scale.get_units(), 3);

It prints number xx.xxx and I want it multiply by 10 000.
Can you help me please :roll_eyes:

Thanks!

Serial.println(10000.0*scale.get_units(), 3);

Thanks for replay, but it does not work for me. Number “3” means how much numbers will be after comma. I did how you showed me, but output was different result. I don not know how to convert this FLOAT function in integer. I need send result through Wireless 2.4GHZ.

#include "HX711.h";
//******************Wireless********************
#include <SPI.h>
#include <RH_NRF24.h>
//******************Wireless********************
#define calibration_factor 38900 //This value is obtained using the SparkFun_HX711_Calibration sketch
//******************Wireless********************
RH_NRF24 nrf24;
//******************Wireless********************


HX711 scale(A1, A0);
void setup() {
  Serial.begin(9600);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare();  //Assuming there is no weight on the scale at start up, reset the scale to 0

  
//******************Wireless********************
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
//******************Wireless********************  

}

void loop() {
 
  Serial.println(scale.get_units(), 3); //scales value
  

  

//******************Wireless********************  
  // Send a message to nrf24_server
   char* msg = "I need get there scale function (scale.get_units(), 3)";

  if (!nrf24.send((uint8_t*)msg, strlen(msg)))
      Serial.println("send failed");   
  nrf24.waitPacketSent();
//******************Wireless********************  
}

I have tried different ways, but always something goes wrong :smiley:

THNAKS!!! :art:

If you want to make a character string from:

  1. a floating point value, use the function dtostrf()
  2. an integer, use either ltoa(), itoa() or utoa() as appropriate.
    e.g.
char msg[10];  //make large enough!
dtostrf(x, 9, 3, msg); //x is a float variable
...or...
itoa(val, msg, 10); //val is a signed int variable

Einars:
Thanks for replay, but it does not work for me. Number "3" means how much numbers will be after comma. I did how you showed me, but output was different result.

What do you mean it was a different result? The number didn't come out like you wanted. There is a good reason for that and you can't get around it. When you multiply a number by 10000 and try to show to 3 decimal places that means there are at least 7 digits in that number. A float can only represent a number to about 6 digits. So there will always be some error. The error comes from the nature of a float and not the function used to multiply it or display it.

Floats are inherently inaccurate. You use them when you need big ranges and have no other choice. If you want accuracy use a long or unsigned long and think in terms of fixed point math and you get 9 digits of precision.

So for example you want meters to 3 decimal places, use millimeters instead and keep it an integer so you can use unsigned long. That gives you accuracy. You can't get that with a float.

The only reason to be using floating point numbers, especially on a micro that can only do 32 bit floats, is if you have numbers to deal with that differ by more than 9 orders of magnitude.