我想将Long Float格式设置为C货币。我想在开头放置一个美元符号,用逗号对小数点前的第三个数字进行迭代,并在小数点前进行一个点运算。 到目前为止,我一直在打印如下数字:
printf("You are owed $%.2Lf!
", money);
返回像
You are owed $123456789.00!
数字应如下所示
$123,456,789.00
$1,234.56
$123.45
任何答案都不必在实际代码中。 您不必喂汤。 如果有与C相关的细节会有所帮助,请提及。 其他伪代码也可以。
谢谢。
见stackoverflow.com/questions/1449805/
也谢谢你 这很有帮助。
我知道这在您的问题范围之外,但是您可能找不到通用的方法,因为将货币值存储为浮点型通常不是一个好主意。
因为可能的精度误差?
+1 @airza。 beProactive-是的,分数在以2为基数/以10为基数的转换中不能很好地发挥作用。 只将美分值存储为整数会好很多。
您的printf可能已经可以通过'标志本身来做到这一点。不过,您可能需要设置区域设置。这是我机器上的一个例子:
#include
#include
int main(void)
{
setlocale(LC_NUMERIC,"");
printf("$%'.2Lf
", 123456789.00L);
printf("$%'.2Lf
", 1234.56L);
printf("$%'.2Lf
", 123.45L);
return 0;
}
并运行它:
> make example
clang -Wall -Wextra -Werror example.c -o example
> ./example
$123,456,789.00
$1,234.56
$123.45
该程序可以在Mac(10.6.8)和我刚刚尝试过的Linux机器(Ubuntu 10.10)上以所需的方式工作。
这会在我的Ubuntu盒子上签出。 谢谢梅恩。
POSIX要求这样做-printf()
我知道这是一条古老的文章,但是今天我消失在man兔子洞的下面,所以我想记录下我的旅行记录:
您可以在monetary.h中包含一个名为strfmon()的函数,该函数可以根据本地或国际标准来执行此操作。
请注意,它的工作方式与printf()类似,并且将采用与字符串中指定的%格式一样多的double自变量。
除了我在这里拥有的功能之外,还有很多其他功能,我发现此页面最有帮助:https://www.gnu.org/software/libc/manual/html_node/Formatting-Numbers.html
#include
#include
#include
#include
int main(){
// need to setlocal(),"" sets locale to the system locale
setlocale(LC_ALL,"");
double money_amt = 1234.5678;
int buf_len = 16;
char simple_local[buf_len];
char international[buf_len];
char parenthesis_for_neg[buf_len];
char specified_width[buf_len];
char fill_6_stars[buf_len];
char fill_9_stars[buf_len];
char suppress_thousands[buf_len];
strfmon(simple_local, buf_len-1,"%n", money_amt);
strfmon(international, buf_len-1,"%i", money_amt);
strfmon(parenthesis_for_neg, buf_len-1,"%(n", money_amt);
strfmon(specified_width, buf_len-1,"%#6n", money_amt);
strfmon(fill_6_stars, buf_len-1,"%=*#6n", money_amt);
strfmon(fill_9_stars, buf_len-1,"%=*#8n", money_amt);
strfmon(suppress_thousands, buf_len-1,"%^=*#8n", money_amt);
printf("===================== Output ===================
"\
"Simple, local: %s
"\
"International: %s
"\
"parenthesis for negatives: %s
"\
"fixed width (6 digits): %s
"\
"fill character '*': %s
"\
"-- note fill characters don't
"\
"-- count where the thousdands
"\
"-- separator would go:
"\
"filling with 9 characters: %s
"\
"Suppress thousands separators: %s
"\
"================================================
",
simple_local, international, parenthesis_for_neg,
specified_width, fill_6_stars, fill_9_stars,
suppress_thousands);
/** free(money_string); */
return 0;
}
===================== Output ===================
Simple, local: $1,234.57
International: USD1,234.57
parenthesis for negatives: $1,234.57
fixed width (6 digits): $ 1,234.57
fill character '*': $**1,234.57
-- note fill characters don't
-- count where the thousdands
-- separator would go:
filling with 9 characters: $*****1,234.57
Suppress thousands separators: $****1234.57
================================================
它也是POSIX功能— strfmon()
我认为没有C函数可以执行此操作,但是您可以自己编写?说float price = 23234.45。首先用逗号打印(int)price,打印小数点;然后对于小数部分,执行printf("%d", (int)(price*100)%100);
标志呢?