dollars.c

来自「c和指针 学习c语言必须阅读的书籍之一 提高对C语言的掌握理解能力」· C语言 代码 · 共 46 行

C
46
字号
/*
** Convert the digit string 'src' to dollars-and-cents form and store
** it in 'dst'.
*/

#include <stdio.h>

void
dollars( register char *dst, register char const *src )
{
	int	len;

	if( dst == NULL || src == NULL )
		return;

	*dst++ = '$';
	len = strlen( src );

	/*
	** If digit string is long enough, copy the digits that will
	** be on the left of the decimal point, putting in commas
	** where appropriate.  If the string is shorter than 3
	** digits, force a '0' into dst ahead of the '.' .
	*/
	if( len >= 3 ){
		int	i;

		for( i = len - 2; i > 0; ){
			*dst++ = *src++;
			if( --i > 0 && i % 3 == 0 )
				*dst++ = ',';
		}
	} else
		*dst++ = '0';

	/*
	** Store the decimal point, and then store the remaining
	** digits from 'src'.  If 'src' had fewer than two digits,
	** force in '0's instead.  Then NUL terminate 'dst'.
	*/
	*dst++ = '.';
	*dst++ = len < 2 ? '0' : *src++;
	*dst++ = len < 1 ? '0' : *src;
	*dst = 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?