将double型的整数部分和小数部分别输出到两个字符串中

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

void FormatDblToStr( double d_in, char * str_int, char * str_decmal );

int main(int argc, char* argv[])
{
    char str_int[8+1];
    char str_decmal[4+1];
 
    memset( str_int, 0x00, sizeof( str_int ) );
    memset( str_decmal, 0x00, sizeof( str_decmal ) );


    double d = 12.34;

    FormatDblToStr( d, str_int, str_decmal);

    printf( "str_int = %s/n", str_int );
    printf( "str_decmal= %s/n", str_decmal );  
    return 0;
}

void FormatDblToStr( double d_in, char * str_int, char * str_decmal )
{
    /* 本地变量的定义 */
    char a[20];
    char b[20];
    char c[20];
    char e[20];

   /* 本地变量初始化 */
    memset( a, 0x00, sizeof( a ) );
    memset( b, 0x00, sizeof( b ) );
    memset( c, 0x00, sizeof( c ) );
    memset( e, 0x00, sizeof( e ) );

   /* 打印整数部分到a中 */
   sprintf( a, "%.0lf", d_in);

   /* 打印全部到b中 */
   sprintf( b, "%.4lf", d_in);

   /* 拷贝b中的小数部分到c中 */
   memcpy( c, b + strlen( a ) + 1,  strlen(b) - strlen(a) -1 );

   /* 打印8位长度的整数 */
   sprintf( e, "%8.0lf", d_in);

   /* 输出结果 */
   memcpy( str_int, e, strlen( e ) );
   memcpy( str_decmal, c, strlen( c ) );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值