Silent_Olive 2021-10-29 21:50 采纳率: 33.3%
浏览 32
已结题

为什么我的数码管显示小数,有时候最后一位会减少“1”,例如 想输出10.15,但4位数码管显示的是10.14

为什么我的数码管显示小数,有时候最后一位会减少“1”,例如 想输出10.15,但4位数码管显示的是10.14
代码如下


#include <STC15F2K60S2.H>
#include <delay.h>
#include <usart.h>

unsigned char Point_Pos;
code unsigned char tab[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
unsigned char Bits[]={0,0,0,0};  
void display()
{    
    int i;
    int j=Point_Pos;
    unsigned char pos=0x7f;
    unsigned char k=0xc0;
    P2=pos;
    
    for(i=0;i<4;i++)
    {
        P0=tab[Bits[i]];
        if(i==(j-1)){P0=P0+0x80;}
        delayxms(1);
        P0=0xff;
        P2=P2^k;
        k=k>>1;
    }
}



void judge_num(double num)   
{
    unsigned long int int_part;       
    double dec_part;           
    int_part=(unsigned long int)num;    
    dec_part=num-int_part;              
    if(int_part<=9)
    {
        Bits[0] = int_part;
        Bits[1] = (unsigned long int)(dec_part * 10);
        Bits[2] = (unsigned long int)(dec_part * 100 - Bits[1] * 10);
        Bits[3] = (unsigned long int)(dec_part * 1000 - Bits[1] * 100 - Bits[2] * 10);
        Point_Pos = 1;
    }
    else if (int_part >= 10 && int_part <= 99)
    {
        Bits[0] = int_part / 10;               
        Bits[1] = int_part - Bits[0] * 10;      
        Bits[2] = (unsigned long int)(dec_part * 10);      
        Bits[3] = (unsigned long int)(dec_part * 100 - Bits[2] * 10);     
        Point_Pos = 2;
    }
    else if (int_part >= 100 && int_part <= 999)
    {
        Bits[0] = int_part / 100;
        Bits[1] = (int_part - Bits[0] * 100) / 10;
        Bits[2] = int_part - Bits[0] * 100 - Bits[1] * 10;
        Bits[3] = (unsigned long int)(dec_part * 10);
        Point_Pos = 3;
    }
    else if (int_part >= 1000 && int_part <= 9999)
    {
        Bits[0] = int_part / 1000;
        Bits[1] = (int_part - Bits[0] * 1000) / 100;
        Bits[2] = (int_part - Bits[0] * 1000 - Bits[1] * 100) / 10;
        Bits[3] = int_part % 10;
        Point_Pos = 0;
    }
}


  • 写回答

1条回答 默认 最新

  • 老皮芽子 2021-10-29 23:49
    关注

    是四舍五入的问题
    调用 judge_num 时
    将 num +0.005就行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月7日
  • 已采纳回答 10月30日
  • 创建了问题 10月29日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号