如何将C ++构建器中的浮子设置为小数分数 我使用C ++构建器,而我的语言环境是欧洲的,所以我有一个逗号作为十进制分离器。 我需要将双重点转换为小数点值,将点作为分离器。 我在任何地方都找不到答案。

问题描述 投票:0回答:2

DecimalSeparator

是一个全局变量,只需将其设置为所需的字符,然后才格式化

double

#include <SysUtils.hpp> System::String FormatWithDot(double value) { System::Char old = Sysutils::DecimalSeparator; Sysutils::DecimalSeparator = _D('.'); System::String s = Sysutils::FloatToStr(value); Sysutils::DecimalSeparator = old; return s; } System::String s = FormatWithDot(123.45);

c++builder
2个回答
4
投票

TFormatSettings::DecimalSeparator

,如果您需要在多个线程中执行此操作,请使用线程安全版本
#include <SysUtils.hpp>

System::String FormatWithDot(double value)
{
    Sysutils::TFormatSettings fmt = Sysutils::TFormatSettings::Create();
    fmt.DecimalSeparator = _D('.');
    return Sysutils::FloatToStr(value, fmt);
}

System::String s = FormatWithDot(123.45);

DecimalSeparator 请注意,

FloatToStr()
仅适用于基于Delphi的RTL函数,例如

Format()

std::(s)printf()
等。
等。对于那些,您需要使用C ++的语言。
    
我本人在早上的凌晨解决了。您可以使用:
std::to_string()
然后格式化这样的两倍:
std::ostream::operator<<
我希望它对某人有帮助。我疯了。

我知道这是一个非常古老的线程,但是我今天面临着这个问题。就我而言,我的解决方案是:

0
投票
TFormatSettings fmt = TFormatSettings::Create(); fmt.DecimalSeparator = '.';

它等同于我用另一种语言所拥有的另一种。对我来说,这是有用的,因为数字是数字参数。

为C ++构建器12.2.

FloatToStr(price, fmt);

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.