Round to first significant digit

so how do you know how many digits you need to print? where was that in your code?

in the code @jfjlaros offered, m is your cue

void setup()
{
  Serial.begin(9600);
  float myNum[] =
  {
    11.871, 11.177, 37.233, 37.779, 0.578, 0.544, 
    0.0257, 0.0223,0.000345 
  };

  for (int i = 0; i < 4; i++)
  {
    Serial.println(myNum[i], 0);
  }
  Serial.println();

  for (int i = 4; i < 6; i++)
  {
    Serial.println(myNum[i], 1);
  }
  Serial.println();
  
  for (int i = 6; i < 8; i++)
  {
    Serial.println(myNum[i], 2);
  }
  Serial.println();
  
  for (int i = 8; i < 9; i++)
  {
    Serial.println(myNum[i], 4);
  }
}

void loop(){}

Output:

12
11
37
38

0.6
0.5

0.03
0.02

0.0003

:rofl: :rofl:
I like your sense of humour :slight_smile:

That's why I did not post my codes at the very first post. I thought that OP will ask me for the codes and the way of saving the results in variables; as a result, I will have some hard tasks to do.

waste of bandwidth and storage :slight_smile:

fabs()

abs() and fabs () functions, both are used to retrieve or calculate the absolute value. The only difference between both of them is, abs() is used to calculate the absolute value for integer type numbers whereas fabs() are used for floating type numbers.

Try this, it just uses the round(number) function:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

float rounded_number = 0;
float number = 0;

void _delay(float seconds) {
  long endTime = millis() + seconds * 1000;
  while(millis() < endTime) _loop();
}

void setup() {
  number = "<Your Number>";
  rounded_number = round(number);

}

void _loop() {
}

void loop() {
  _loop();
}

really ???

This is apparently how it is implemented.

#define abs(x) ((x)>0?(x):-(x))

post # number, please!

post #5 #8 or #10

float myRound(float f) {
  if (f == 0) return 0;

  float m = 1;
  while (abs(f * m) < 1)  m *= 10;
  return round(f * m) / m;
}

I have uploaded that sketch @jfjlaros ; but, the outcome does not match with what OP wants.

float myNum[] =
{
  11.871, 11.177, 37.233, 37.779, 0.578, 0.544,
  0.0257, 0.0223, 0.000345
};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 9; i++)
  {
    float x = myRound(myNum[i]);
    Serial.println(x, 4);
  }
}

void loop()
{
}

float myRound(float f)
{
  if (f == 0)
    return 0;
  float m = 1;

  while (abs(f * m) < 1)
  {
    m *= 10;
  }
  return round(f * m) / m;
}

Output:

12.0000
11.0000
37.0000
38.0000
0.6000
0.5000
0.0300
0.0200
0.0003

OP wants:

12
11
37
38
0.60
0.50
0.03
0.02
0.0003

as you said, OP did not say if he wants a variable or a print out

with float anyway some numbers are not representable and so any math you do with them leads to approximation

Indeed, plus I would argue that formatting the result should be done in a different function anyway.

to be completely honest, m in this code is not what mathematically we would expect it to be

float myRound(float f) {
  if (f == 0) return 0;

  float m = 1;
  while (abs(f * m) < 1)  m *= 10;
  return round(f * m) / m;
}

as the float representation of f might not match the true value that was originally provided. So you are already on moving sand from the start

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.