Saving a 3 decimal value from HX711

Hi guys! First post around here, however a “long” follower of the forum since my journey with arduino’s started :smiley:

So, by now I’m using a 20kg load cell and hx711 amplifier.

I’m using the hx711 library and calibration runned perfectly and I can read values up to 3 decimals with the following code:

#include <HX711.h>

#define DOUT 3
#define CLK 2

HX711 scale(DOUT, CLK);

float calibration_factor = -96650;

void setup() {
Serial.begin(9600);
Serial.println(“Press T to tare”);
scale.set_scale(-96650); //Calibration Factor
scale.tare(); //Reset the scale to 0
}

void loop() {
Serial.print(“Weight: “);
Serial.print(scale.get_units(), 3); //Up to 3 decimal points
Serial.println(” kg”);

if(Serial.available())
{
char temp = Serial.read();
if(temp == ‘t’ || temp == ‘T’)
scale.tare(); //Reset the scale to zero
}
}

However, I wish to work with the value that is shown and I tried to use:

int w = scale.get_units();

But when I run the command like that I just get a value with 2 decimal instead of 3.

How can I save the value with 3 decimal too, in my variable?

Thanks in advance and have a nice weekend! 8)

What you save it with and what you print it with are two different things.

And despite being a long-time follower of the forum, you still managed to miss out using code tags.

int w = scale.get_units();

But when I run the command like that I just get a value with 2 decimal instead of 3.

That line gave you a number in w with decimals? That a really neat trick. Can you show us how you did that and include the code and the output? Maybe instead of making stuff up that you think you did, let’s get really precise with this. That’s how code works. Let’s talk about what REALLY happened, not some approximation of what you think you did.

Next, you need to learn the difference between a number, which is an abstract concept and doesn’t have any fixed number of decimal places, and the representation of a number, which is concrete and what you write down and does have some fixed number of decimals. Next you need to look at how a float is stored, it doesn’t have ANY decimals. It is stored as a whole number part and a fraction part. The decimals are put there when you print it out because humans are really crappy at understanding fractions in binary. It doesn’t have some number of decimal places stored with it. The number of decimal places thing only matters when you print it.