简体   繁体   English

删除浮点值的尾随零 C++

[英]removing trailing zeroes for a float value c++

I am trying to set up a nodemcu module to collect data from a temperature sensor, and send it using mqtt pubsubclient to my mqtt broker, but that is not the problem.我正在尝试设置一个 nodemcu 模块以从温度传感器收集数据,并使用 mqtt pubsubclient 将其发送到我的 mqtt 代理,但这不是问题所在。

I am trying to send the temperature in a format that only has one decimal, and at this point I've succesfully made it round up or down, but the format is not right.我试图以只有一位小数的格式发送温度,此时我已经成功地将它向上或向下舍入,但格式不正确。 as of now it rounds the temp to 24.50, 27.80, 23.10 etc. I want to remove the trailing zereos, so it becomes 24.5, 27.8, 23.1 etc.截至目前,它将温度四舍五入为 24.50、27.80、23.10 等。我想删除尾随的 zereos,因此它变成 24.5、27.8、23.1 等。

I have this code set up so far:到目前为止我已经设置了这段代码:

#include <math.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>


float temp = 0;

void loop {
  float newTemp = sensors.getTempCByIndex(0);

  temp = roundf((newTemp * 10)) / 10;
  serial.println(String(temp).c_str())
  client.publish("/test/temperature", String(temp).c_str(), true);

}

I'm fairly new to c++, so any help would be appreciated.我是 c++ 的新手,所以我们将不胜感激。

It's unclear what your API is.目前还不清楚你的 API 是什么。 Seems like you want to pass in the C string.似乎您想传入 C 字符串。 In that case just use sprintf :在这种情况下,只需使用sprintf

#include <stdio.h>

float temp = sensors.getTempCByIndex(0);
char s[30];
sprintf(s, "%.1f", temp);
client.publish("/test/temperature", s, true);

Regardless of what you do to them, floating-point values always have the same precision.无论您对它们做什么,浮点值始终具有相同的精度。 To control the number of digits in a text string, change the way you convert the value to text.要控制文本字符串中的位数,请更改将值转换为文本的方式。 In normal C++ (ie, where there is no String type <g>), you do that with a stream:在普通的 C++ 中(即没有String类型 <g> 的地方),您可以使用流来实现:

std::ostrstream out;
out << std::fixed << std::setprecision(3) << value;
std::string text = out.str();

In the environment you're using, you'll have to either use standard streams or figure out what that environment provides for controlling floating-point to text conversions.在您使用的环境中,您必须要么使用标准流,要么弄清楚该环境提供了什么来控制浮点到文本的转换。

The library you are using is not part of standard C++.您使用的库不是标准 C++ 的一部分。 The String you are using is non-standard.您使用的String是非标准的。

As Pete Becker noted in his answer, you won't be able to control the trailing zeros by changing the value of temp .正如皮特贝克尔在他的回答中指出的那样,您将无法通过更改temp的值来控制尾随零。 You need to either control the precision when converting it to String , or do the conversion and then tweak the resultant string.您需要在将其转换为String时控制精度,或者进行转换然后调整结果字符串。

If you read the documentation for the String type you are using, there may be options do do one or both of;如果您阅读了您正在使用的String类型的文档,可能会有选项执行其中一项或两项;

  • control the precision when writing a float to a string;控制将float写入字符串时的精度; or要么
  • examine characters in a String and manually remove trailing zeros.检查字符串中的String并手动删除尾随零。

Or you could use a std::ostrstream to produce the value in a std::string , and work with that instead.或者您可以使用std::ostrstream来生成std::string中的值,然后使用它来代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM