C ++ printf与%f但本地化为用户的国家 [英] C++ printf with %f but localized for the user's country

查看:153
本文介绍了C ++ printf与%f但本地化为用户的国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下C ++语法在Windows平台上输出浮点值:

I'm using the following C++ syntax to output a floating point value on a Windows platform:

printf("%.2f", 1.5);

如果我在英文用户帐户上运行它可以很好。我的假设是,如果我运行它,说法语用户帐户,输出将是1.50而不是1.50。

It works well if I run it on an English user account. My assumption was that if I run it on, say French user account, the output will be 1,50 instead of 1.50.

为什么我看不到它,如何

Why do I not see it and how to produce my desired result?

推荐答案

基数字符(即'。'或',')由当前语言环境定义。默认语言环境(至少对于Windows系统)是C,它将。定义为基数字符。

The radix character (i.e. '.' or ',') is defined by the current locale. The default locale (at least for Windows systems) is "C", which defines '.' as radix character.

您可以设置C / C ++程序使用 setlocale 函数。

You can set the current locale for a C/C++ program using the setlocale function.

要将语言环境设置为当前系统/用户语言环境,以下语句:

To set the locale to the current system/user locale, you can use the following statement:

#include <locale.h>
setlocale(LC_ALL, ".OCP");

请参阅在这里(参见链接页面上的示例...)了解有关 setlocale 的更多信息。

See here (cf. the examples on the linked page...) for more information about setlocale

这篇关于C ++ printf与%f但本地化为用户的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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