-3
float Vout, Tav, To, TempValue;

Vout = sensor.Sense();          // Sample data (read sensor)
Tav  = sensor.GetAverageTemp(); // Calculate average temperature from N samples
To   = sensor.GetLatestTemp();  // Calculate temperature from the latest sample

TempValue = sensor.GetAverageTemp();

pc.printf("Temp Value: %.1f", TempValue);//<-- Outputs Temp Value: 25.6

printf successfully outputs the value up to the tenths place. How do I save the value up to the tenths place to the TempValue variable in C++?

I attempted to use floor():

TempValue = sensor.GetAverageTemp();
pc.printf("Temp Value before floor: %f\n\r", TempValue);
TempValue = floor(TempValue*100)/100;
pc.printf("Temp Value after floor: %f\n\r", TempValue);

That code resulted in:

Temp Value before floor: 25.731195

Temp Value after floor: 25.730000

I am almost there, however, I now have trailing zeros that I would like to remove.

dottedquad
  • 1,371
  • 6
  • 24
  • 55
  • 1
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – v.oddou Feb 25 '15 at 01:23
  • What do you mean by "save the value"? If you want to print the value you could use the printf() function in C++ as well as C. See http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c – Richard Chambers Feb 25 '15 at 01:31
  • `TempValue = sensor.GetAverageTemp();` for example, has a value of: 24.822018 I would like to save the value to: 24.8 – dottedquad Feb 25 '15 at 01:34
  • 1
    So what you want to do is to perform rounding to the nearest tenth? Take a look at the math functions at `round()`, `trunc()`, etc. http://stackoverflow.com/questions/485525/round-for-float-in-c – Richard Chambers Feb 25 '15 at 01:38

2 Answers2

3

You're almost certainly solving the wrong problem.

It rarely makes sense to round a floating-point value to some number of decimal places. You can do some arithmetic to take the value 24.822018 and generate a close approximation of 24.8, but since floating-point is (usually) stored in binary, it can't represent 24.8 exactly. The actual stored value will likely be something like

24.800000000000000710542735760100185871124267578125

You already know how to print a floating-point value truncated to a specified number of decimal places (using printf with a "%.1f" format) -- or you can use std::setprecision, since you're asking about C++. (But std::setprecision specifies the total number of digits, not the digits after the decimal point.) In almost all cases, there's no benefit in truncating a number before you're ready to print it.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

I believe you are looking for the std::setprecision from iomanip.

Sample usage would be:

std::cout << std::setprecision(2) << TempValue << std::endl;

If however, you are attempting to round a variable, something along the lines of:

void round_to_tenths(float &f) {
  uint32_t val = (uint32_t) f;
  float dec = f - val;
  uint32_t sd = (uint32_t)(dec * 100);
  f = val;
  uint8_t tmp;
  uint32_t test = sd % 10;
  sd /= 10;
  if (test >= 5) {
    tmp = sd % 10 + 1;
  } else {
    tmp = sd % 10;
  }
  f += (float) tmp / 10;
}

should work, however you still have to add checks for the negative numbers.

  • You beat me to the answer :-) I was able to set the precision to 24.8 by writing: `std::cout << std::setprecision(3) << TempValue << std::endl;` I changed the 2 to a 3. – dottedquad Feb 25 '15 at 01:50
  • 2
    But this doesn't answer the question that you asked. You asked about saving the value "to the `TempValue` variable", not about printing it (which the code in your question already does). Can you update your question to make it clearer what you're actually asking? – Keith Thompson Feb 25 '15 at 02:05
  • @KeithThompson For some strange reason it 'seemed' as if it worked for limiting the numbers after the decimal. I ended up removing all the code and just compiled: `std::cout << std::setprecision(3) << TempValue << std::endl; pc.printf("Temp Value: %f", TempValue);` cout behaves just as pc.printf. I was under the assumption cout would update the variable value. My assumption is wrong after reading about cout. So, now I am back to my original question. How do I limit the leading numbers after the decimal to only the tenths digit I.E. 25.9 from the original value of: 25.979151? – dottedquad Feb 27 '15 at 02:37
  • @dottedquad: Which goes back to my question: Why would you want to do that? You *can't* store the exact value `25.9` in a `float` object. The only context I can think of where it makes sense to do that is on output, and that's more easily done by formatting the output, not by modifying the `float` value. (And `setprecision` controls the total number of digits, not the number of digits after the decimal point.) See also [my answer](http://stackoverflow.com/a/28709748/827263). – Keith Thompson Feb 27 '15 at 03:10
  • @KeithThompson I now see and understand your explanation. It is now pointless for me to do what I have been asking. After all, all I am really trying to do is store the value of a temperature sensor and other sensors and form a JSON string and send the JSON string to a graph to plot the points. I will now just 'edit' the float value for the purpose of visualizing the data. – dottedquad Feb 27 '15 at 04:30