歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux服務器

編輯數值金額成中文金額

/*

    * Function Name: edit_money

    * Description :編輯數值金額成中文金額

    * Input : char * in --輸入的數值字符串

    *

    * Output : char * out --輸出的中文金額

    * Return :

    */

    void

    edit_money( char * in, char * out )

    {

    char    s1[256], s2[ 10 ]; int     i, j, k, l, n, len1;

    char * p_dot;

    char    chn_digit[11][3] = { "零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖" }; char    chn_unit1[4][3] = { "", "拾", "佰", "仟" }; char    chn_unit2[3][5] = { "萬", "億", "萬億" };

    if ( in == NULL || out == NULL )

    return;

    if ( strlen( in ) == 0 )

    return;

    memset(s1,0x00,sizeof(s1)); strncpy( s1, in, sizeof(s1) );

    /*判斷是否有小數點*/

    p_dot = strchr( s1, '.' );

    if ( p_dot == NULL )

    s2[0] = 0x0;

    else

    {

    memset(s2,0x00,sizeof(s2)); strcpy( s2, p_dot + 1 );

    *p_dot = 0x0;

    }

    /*

    *轉換為中文

    */

    out[0] = 0x0;

    len1 = strlen( s1 );

    n = ( len1 - 1 ) / 4 + 1;

    l = 0;

    for ( i = 0; i < n; i++ )

    {

    if ( i == 0 )

    k = len1 - 4 * ( n - 1 );

    else

    k = 4;

    for ( j = 0; j < k; j++, l++ )

    {

    if ( s1[ l ] == '0' &&

    ( ( j + 1 < k && s1[ l+1 ] == '0' ) ||

    j + 1 == k ) )

    continue;

    /* 100000.00顯示為:壹拾萬元 */ strcat( out, chn_digit[ s1[ l ] - '0' ] ); if ( s1[ l ] != '0' ) strcat( out, chn_unit1[ k - j - 1 ] );

    }

    /* 20030921 modify:(4*i -> l-k), add:(i==0) if ( i < n - 1 && memcmp( s1 + 4*i, "0000", k ) != 0 ) strcat( out, chn_unit2[ n - i - 2 ] );

    */

    if( ( i < n - 1 ) && \

    ( i == 0 || memcmp( s1+l-k, "0000", k ) != 0 ) ) strcat( out, chn_unit2[ n - i - 2 ] );

    }

    if ( strlen( out ) !=

0 ) strcat( out, "元" );

    if ( atoi( s2 ) == 0 )

    strcat( out, "整" );

    else

    {

    /* 個位是零或角位是零, 20030921 add:(s2[0] == '0' ) */

    if( ( len1 > 1 && s1[ len1 - 1 ] == '0' ) \

    || s2[0] == '0' )

    strcat( out, "零" ); if ( s2[0] != '0' )

    {

    strcat( out, chn_digit[ s2[0] - '0' ] ); strcat( out, "角" );

    }

    if ( s2[1] != '0' && s2[1] != 0 )

    {

    strcat( out, chn_digit[ s2[1] - '0' ] ); strcat( out, "分" );

    }

    }

    return;

    }

Copyright © Linux教程網 All Rights Reserved