Hi,
I have a value of -7.284392. How to take first 5 digits from that value? (-7.2843)
I've tried using round(-7.284392*10000)/10000, but it becomes a problem when the sixth number >= 5, then the fifth number is rounded up.
Hi,
I have a value of -7.284392. How to take first 5 digits from that value? (-7.2843)
I've tried using round(-7.284392*10000)/10000, but it becomes a problem when the sixth number >= 5, then the fifth number is rounded up.
What data type is the value in and where does it come from ?
Do you want to use the value of the first 5 digits or just print them ?
round((-7.284392-0.000005)*10000)/10000
Yes, i want to use it for if statement
Then do you need to truncate it ?
What are you you comparing it with in the if statement ?
Please post an example of the comparison
do you need to round? up/down/etc...
https://www.cplusplus.com/reference/cmath/round/
or just chop off last unneeded digit ? %/10
float valA;
char floatstr [10];
float redoA;
void setup()
{
Serial.begin(19200);
valA = -7.284392;
dtostrf(valA,10,5,floatstr);
redoA = atof(floatstr);
Serial.print("floatstr = ");
Serial.println(floatstr);
Serial.print("redoA = ");
Serial.println(redoA, 5);
}
If I change the last line to
Serial.println(redoA, 7);
then I get --
floatstr = -7.28439
redoA = -7.2843894
Just another XY problem.
Hi, @hitzzer
Welcome to the forum.
Please read the post at the start of any forum , entitled "How to use this Forum".
This will help with advice on how to present your code and problems.
Why exactly do you need to truncate the value?
What is your project?
Thanks.. Tom...
![]()