[英]fixed point decimal printf output
I have my LCD working on my TM4C123 and I have printf working. 我的液晶显示器在我的TM4C123上工作,我有printf工作。
I'm trying to make the input from the left column look like the output from the right column. 我正在尝试使左列的输入看起来像右列的输出。
What I tried was to extract each digit from the integer and then print each one out and then put the decimal somewhere in there. 我尝试的是从整数中提取每个数字,然后打印出每个数字,然后将小数放在那里。 When I do extract the digits, the sign seems to stick with the digit and I just can't seem to get it formatted right.
当我提取数字时,符号似乎坚持数字,我似乎无法正确格式化。
outTestCaseType1 outTests1[13]={
{ 0, " = 0.000?\r" }, // 0/1000 = 0.000
{ 4, " = 0.004?\r" }, // 4/1000 = 0.004
{ -5, " = -0.005?\r" }, // -5/1000 = -0.005
{ 78, " = 0.078?\r" }, // 78/1000 = 0.078
{ -254, " = -0.254?\r" }, // -254/1000 = -0.254
{ 999, " = 0.999?\r" }, // 999/1000 = 0.999
{ -1000, " = -1.000?\r" }, // -1000/1000 = -1.000
{ 1234, " = 1.234?\r" }, // 1234/1000 = 1.234
{ -5678, " = -5.678?\r" }, // -5678/1000 = -5.678
{ -9999, " = -9.999?\r" }, // -9999/1000 = -9.999
{ 9999, " = 9.999?\r" }, // 9999/1000 = 9.999
{ 10000, " = *.***?\r" }, // error
{-10000, " = *.***?\r" } // error
};
My code is as follows: 我的代码如下:
void ST7735_sDecOut3(int32_t n) {
int32_t msd,nsd, lsd;
int32_t value;
value=n;
if (value > 999)
{
value = 999;
}
msd = value / 100;
value -= msd * 100;
nsd = value / 10;
value -= nsd * 10;
lsd = value;
printf("%d%d%d", msd, nsd, lsd);
}
The output is as follows on the LCD: LCD上的输出如下:
I'm picking at this as much as I can and this is as close as I got so far. 我尽可能多地接受这个,这和我到目前为止一样接近。 There could be an easier way that I don't see because of my inexperience but I'll keep trying because that's all I can do.
由于我的经验不足,我可能会有一种更容易看到的方法,但我会继续努力,因为这就是我所能做的。
Any hints or help will be appreciated. 任何提示或帮助将不胜感激。
This doesn't use your data structure or types, but can be readily adapted to match. 这不会使用您的数据结构或类型,但可以很容易地进行匹配。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int values[] = { 0, 4, -5, 78, -254, 999, -1000, 1234, -5678, -9999, +9999, 10000, -10000 };
enum { NUM_VALUES = sizeof(values) / sizeof(values[0]) };
for (int i = 0; i < NUM_VALUES; i++)
{
int32_t value = values[i];
char integer[2];
char fraction[4];
int f_part = abs(value) % 1000;
int i_part = abs(value) / 1000;
char sign = ' ';
if (abs(value) > 9999)
{
strcpy(integer, "*");
strcpy(fraction, "***");
}
else
{
if (value < 0)
sign = '-';
sprintf(integer, "%d", i_part);
sprintf(fraction, "%.3d", f_part);
}
printf("%6d: = %c%s.%s?\\r\n", value, sign, integer, fraction);
}
return 0;
}
Example output: 示例输出:
0: = 0.000?\r
4: = 0.004?\r
-5: = -0.005?\r
78: = 0.078?\r
-254: = -0.254?\r
999: = 0.999?\r
-1000: = -1.000?\r
1234: = 1.234?\r
-5678: = -5.678?\r
-9999: = -9.999?\r
9999: = 9.999?\r
10000: = *.***?\r
-10000: = *.***?\r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.