5

我正在寻找将 Long Float 格式化为 C 中的货币。我想在开头放置一个美元符号,逗号迭代小数点前的每三个数字,并在小数点前立即放置一个点。到目前为止,我一直在打印这样的数字:

printf("You are owed $%.2Lf!\n", money);

返回类似的东西

You are owed $123456789.00!

数字应该是这样的

$123,456,789.00
$1,234.56
$123.45

任何答案都不需要在实际代码中。你不必用勺子喂食。如果有与 c 相关的细节会有所帮助,请提及。其他伪代码很好。

谢谢。

4

3 回答 3

11

printf可能已经能够使用'标志自己做到这一点。不过,您可能需要设置您的语言环境。这是我机器上的一个例子:

#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) 上都可以按照您希望的方式运行。

于 2012-07-27T20:21:10.360 回答
2

我知道这是一个很老的帖子,但我今天消失在man兔子洞里,所以我想我会记录下我的旅行:

strfmon()您可以包含一个名为的函数monetary.h来执行此操作,并根据本地或国际标准执行此操作。

请注意,它的工作方式类似于printf(),并且将采用与字符串中指定的格式一样多double的参数。%

它比我这里的要多得多,我发现这个页面是最有帮助的:https ://www.gnu.org/software/libc/manual/html_node/Formatting-Numbers.html

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

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 ===================\n"\
            "Simple, local:                  %s\n"\
            "International:                  %s\n"\
            "parenthesis for negatives:      %s\n"\
            "fixed width (6 digits):         %s\n"\
            "fill character '*':             %s\n"\
            "-- note fill characters don't\n"\
            "-- count where the thousdands\n"\
            "-- separator would go:\n"\
            "filling with 9 characters:      %s\n"\
            "Suppress thousands separators:  %s\n"\
            "================================================\n",
            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
================================================
于 2019-07-13T21:02:27.950 回答
0

我认为没有 C 函数可以做到这一点,但您可以自己编写吗?说float price = 23234.45。先(int)price用逗号打印,打印小数点;然后对于小数部分,做printf("%d", (int)(price*100)%100);

于 2012-07-27T20:16:37.923 回答