本帖最后由 734774645 于 2024-1-10 15:32 编辑
原来的函数是这样的
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Display a float
* @param x position x-axis 0 - 127
* @param y position y-axis 0
* @param num The order of the Chinese characters in the Hzb[] array
* @param accuracy Preserve decimal places
* @param fontsize 8/16
* [url=home.php?mod=space&uid=536309]@NOTE[/url]
*/
void OLED_ShowFloat(uint8_t x, uint8_t y, float num, uint8_t accuracy, uint8_t fontsize)
{
uint8_t i = 0;
uint8_t j = 0;
uint8_t t = 0;
uint8_t temp = 0;
uint16_t numel = 0;
uint32_t integer = 0;
float decimals = 0;
//Is a negative number?
if (num < 0)
{
OLED_ShowChar(x, y, '-', fontsize);
num = 0 - num;
i++;
}
integer = (uint32_t)num;
decimals = num - integer;
//Integer part
if (integer)
{
numel = (uint16_t)integer;
while (numel)
{
numel /= 10;
j++;
}
i += (j - 1);
for (temp = 0; temp < j; temp++)
{
OLED_ShowChar(x + 8 * (i - temp), y, integer % 10 + '0', fontsize); // 显示整数部分
integer /= 10;
}
}
else
{
OLED_ShowChar(x + 8 * i, y, temp + '0', fontsize);
}
i++;
//Decimal part
if (accuracy)
{
OLED_ShowChar(x + 8 * i, y, '.', fontsize);
i++;
for (t = 0; t < accuracy; t++)
{
decimals *= 10;
temp = (uint8_t)decimals;
OLED_ShowChar(x + 8 * (i + t), y, temp + '0', fontsize);
decimals -= temp;
}
}
}
当我要显示28.55的时候显示成了28.54,大家猜猜是哪儿的错误。
|
@mxkw0514 :是的,我打错了。是0.01
这个好像差0.01,跟楼主说的不一样