我怎么能在C逗号格式化货币? [英] How can I format currency with commas in C?

查看:112
本文介绍了我怎么能在C逗号格式化货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待格式化长期持股量货币C.我想放置一个美元符号开头,逗号迭代小数点前每三个数字,小数点前立即点。到目前为止,我已经印刷数字像这样:

I'm looking to format a Long Float as currency in C. I would like to place a dollar sign at the beginning, commas iterating every third digit before decimal, and a dot immediately before decimal. So far, I have been printing numbers like so:

的printf(你欠$%2LF \\ n!钱);

返回服用点像

你欠$ 123456789.00!

数字这个样子

$ 123,456,789.00

$ 1,234.56

$ 123.45

任何答案不一定是实际code。您不必到勺子喂。如果有C相关的细节这将有所帮助,请提及。否则伪code是罚款。

Any answers need not be in actual code. You don't have to spoon feed. If there are c-related specifics which would be of help, please mention. Else pseudo-code is fine.

感谢。

推荐答案

的printf 可能已经能够做到这一点本身与 标记。你可能需要设置您的区域,虽然。下面是我的机器的例子:

Your printf might already be able to do that by itself with the ' flag. You probably need to set your locale, though. Here's an example from my machine:

#include <stdio.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_NUMERIC, "");
    printf("$%'.2Lf\n", 123456789.00L);
    printf("$%'.2Lf\n", 1234.56L);
    printf("$%'.2Lf\n", 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),你希望它无论是方式我只是尝试。

This program works the way you want it to both on my Mac (10.6.8) and on a Linux machine (Ubuntu 10.10) I just tried.

这篇关于我怎么能在C逗号格式化货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆