C++ Builder 参考手册 ➙ System::Sysutils ➙ FormatFloat
格式化浮点数,把浮点数按照参数给定格式转成字符串
头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:
System::UnicodeString __fastcall FormatFloat(
const System::UnicodeString Format,
System::Extended Value);
System::UnicodeString __fastcall FormatFloat(
const System::UnicodeString Format,
System::Extended Value,
const TFormatSettings &AFormatSettings);
参数:
- Value:浮点数;
- Format:格式字符串,详见本文后面的表格;
- AFormatSettings:地区格式;
返回值:
- 把参数 Value 的值按照参数 Format 的格式转为字符串;
- 地区格式:这个函数使用了地区格式的 DecimalSeparator 成员作为小数点、ThousandSeparator 成员作为千位分隔符,不同的地区可能会使用不同的字符当做小数点和千位分隔符,中国和大多数国家一样使用小圆点作为小数点、逗号作为千位分隔符,但是有的国家 - 例如法国:使用逗号当做小数点、空格当做千位分隔符,如果程序在法国和越南等国家的电脑上运行,默认情况所有的小数点都会使用逗号的,包括浮点数和货币型,程序国际化时要特别注意;
- 如果有 AFormatSettings 参数,使用这个参数的格式;
- 如果没有 AFormatSettings 参数,使用 全局变量 System::Sysutils::FormatSettings 作为地区格式;
- AFormatSettings 或 System::Sysutils::FormatSettings 的例子请参考 FloatToStrF
- 没有 AFormatSettings 参数的函数不是线程安全的,因为使用了全局变量作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。
Format 格式:
控制符 | 说明 |
---|---|
0 |
这个位置必须输出数字,如果没有输出,用 0 填补 |
# |
这个位置如果没有数字输出就不输出,不用填补 |
. |
输出小数点。格式字符串里面第一个出现的 '.' 认为是小数点位置,其他的 '.' 被忽略 |
, |
使用千分位分割符。格式字符串里面只要 ',' 就会使用千分位分割符,位置和个数不影响输出效果 |
E+ 或 e+
|
使用科学计数法,后面可以紧跟着最多 4 个 '0' ,表示指数的位数,指数无论是正数还是负数都输出符号 + 或 -
|
E- 或 e-
|
使用科学计数法,后面可以紧跟着最多 4 个 '0' ,表示指数的位数,指数正数不输出符号,指数是负数输出符号 -
|
'文字' 或 \"文字\"
|
单引号和双引号里面的文字直接原样输出 |
; |
如果格式字符串里面包含了分号 ';' ,说明正数、负数和零采用不同的格式,分号 ';' 是分割正数、负数和零的格式之间的分割符:没有分割符:所有的数字采用同一种格式; 有1个分割符:前面部分是正数和0,后面部分是负数的格式; 有2个分割符:前面部分是正数;中间是0;后面是负数的格式 |
格式输出的例子:
格式↓ 输出↘ 数值→ | 12345.6789 |
0 |
-12345.6789 |
---|---|---|---|
"0.00" |
12345.68 | 0.00 | -12345.68 |
"0.000" |
12345.679 | 0.000 | -12345.679 |
"#,0.00" |
12,345.68 | 0.00 | -12,345.68 |
"#,##0.00" |
12,345.68 | 0.00 | -12,345.68 |
"000000.000" |
012345.679 | 000000.000 | -012345.67 |
"000,000.000" |
012,345.679 | 000,000.000 | -012,345.679 |
"0.000E+00" |
1.235E+04 | 0.000E+00 | -1.235E+04 |
"'正'0.00;'负'0.00;零" |
正12345.68 | 零 | 负12345.68 |
"0" |
12346 | 0 | -12346 |
"0.###" |
12345.679 | 0 | -12345.679 |
"0.######" |
12345.6789 | 0 | -12345.6789 |
"0.000###" |
12345.6789 | 0.000 | -12345.6789 |
"0.000000" |
12345.678900 | 0.000000 | -12345.678900 |
注:小数点和千分位符会被 AFormatSettings 参数或全局变量 System::Sysutils::FormatSettings 的 DecimalSeparator 和 ThousandSeparator 成员替代,而不是始终使用 '.'
和 ','
,默认情况,采用的是当前地区的格式,比如在法国,小数点会使用逗号 ','
而不是小圆点。地区格式的具体例子请参考 FloatToStrF
相关:
- System::Sysutils::FloatToStr
- System::Sysutils::FloatToStrF
- System::Sysutils::FloatToText
- System::Sysutils::FloatToTextFmt
- System::Sysutils::FloatToDecimal
- System::Sysutils::FloatToCurr
- System::Sysutils::TryFloatToCurr
- System::Sysutils::TextToFloat
- System::Sysutils::StrToFloat
- System::Sysutils::StrToFloatDef
- System::Sysutils::TryStrToFloat
- System::Sysutils::StrToCurr
- System::Sysutils::StrToCurrDef
- System::Sysutils::TryStrToCurr
- System::Sysutils::CurrToStr
- System::Sysutils::CurrToStrF
- System::Sysutils::FormatFloat
- System::Sysutils::FormatCurr
- System::Sysutils::TFloatRec
- System::Sysutils::TFloatValue
- System::Sysutils::FormatSettings
- System::Sysutils::TFormatSettings
- System::Sysutils::StrToBool
- System::Sysutils::StrToBoolDef
- System::Sysutils::TryStrToBool
- System::Sysutils::BoolToStr
- System::Sysutils::DateTimeToStr
- System::Sysutils::DateTimeToString
- System::Sysutils::DateToStr
- System::Sysutils::GUIDToString
- System::Sysutils::IntToStr
- System::Sysutils::IntToHex
- System::Sysutils::TimeToStr
- System::Sysutils::UIntToStr
- System::Sysutils
- System::Currency
- System
- std::itoa, std::_itoa, std::_itot, std::_itow
- std::ltoa, std::_ltoa, std::_ltot, std::_ltow
- std::ultoa, std::_ultoa, std::_ultot, std::_ultow
- std::_i64toa, std::_i64tot, std::_i64tow
- std::_ui64toa, std::_ui64tot, std::_ui64tow
- std::ecvt, std::_ecvt
- std::fcvt, std::_fcvt
- std::gcvt, std::_gcvt
- <cstdlib>
C++ Builder 参考手册 ➙ System::Sysutils ➙ FormatFloat
`