- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在Delphi1中,使用FloatToStrF
或CurrToStrF
将自动使用DecimalSeparator
字符来表示小数点。不幸的是DecimalSeparator
is declared in SysUtils as Char
1,2:
var
DecimalSeparator: Char;
虽然 LOCALE_SDECIMAL
最多允许三个字符:
Character(s) used for the decimal separator, for example, "." in "3.14" or "," in "3,14". The maximum number of characters allowed for this string is four, including a terminating null character.
这会导致Delphi无法正确读取小数点分隔符;回退到默认的小数点分隔符“.
”:
DecimalSeparator := GetLocaleChar(DefaultLCID, LOCALE_SDECIMAL, '.');
在我的计算机 which is quite a character 上,这会导致浮点和货币值错误地使用 U+002E(句号)小数点进行本地化。
我愿意直接调用 Windows API 函数,这些函数旨在将浮点或货币值转换为本地化字符串:
除了这些函数采用一串图片代码,其中唯一允许的字符是:
U+0030
..U+0039
)U+002E
),则为一个小数点 (.
)U+002D
)将浮点或货币值转换为遵守这些规则的字符串的好方法是什么1?例如
1234567.893332
-1234567
假设本地用户的区域设置(即我的计算机):
-
来表示否定(例如 --
).
to indicate a decimal point(例如 ,,
)0123456789
to represent digits(例如[删除了导致 SO javascript 解析器崩溃的阿拉伯数字])一个可怕的、可怕的黑客,我可以使用:
function FloatToLocaleIndependantString(const v: Extended): string;
var
oldDecimalSeparator: Char;
begin
oldDecimalSeparator := SysUtils.DecimalSeparator;
SysUtils.DecimalSeparator := '.'; //Windows formatting functions assume single decimal point
try
Result := FloatToStrF(Value, ffFixed,
18, //Precision: "should be 18 or less for values of type Extended"
9 //Scale 0..18. Sure...9 digits before decimal mark, 9 digits after. Why not
);
finally
SysUtils.DecimalSeparator := oldDecimalSeparator;
end;
end;
<小时/>
有关 VCL 使用的函数链的其他信息:
FloatToStrF
和 CurrToStrF
调用:
注意
DecimalSeparator:Char
,单个 character global is deprecated ,并替换为另一个单字符小数分隔符1 在我的 Delphi 版本中
2 以及当前版本的 Delphi
最佳答案
Delphi确实提供了一个名为FloatToDecimal
的过程,它将浮点(例如扩展
)和货币
值转换为有用的结构以进一步格式化。例如:
FloatToDecimal(..., 1234567890.1234, ...);
给你:
TFloatRec
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = 10
IsNegative: Boolean = True
其中指数
给出小数点左边的位数。
有一些特殊情况需要处理:
指数为零
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = 0
IsNegative: Boolean = True
表示小数点左边没有数字,例如.12345678901234
指数为负
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = -3
IsNegative: Boolean = True
表示您必须在小数点和第一位数字之间添加零,例如.00012345678901234
指数为-32768
(NaN,不是数字)
Digits: array[0..20] of Char = ""
Exponent: SmallInt = -32768
IsNegative: Boolean = False
表示该值不是数字,例如NAN
指数为 32767
(INF 或 -INF)
Digits: array[0..20] of Char = ""
Exponent: SmallInt = 32767
IsNegative: Boolean = False
表示该值为正无穷大或负无穷大(取决于 IsNegative
值),例如-INF
我们可以使用FloatToDecimal
作为起点来创建与区域设置无关的“图片代码”字符串。
然后可以将此字符串传递给适当的 Windows GetNumberFormat
或 GetCurrencyFormat
函数以执行实际的正确本地化。
我编写了自己的 CurrToDecimalString
和 FloatToDecimalString
,它们将数字转换为所需的区域设置独立格式:
class function TGlobalization.CurrToDecimalString(const Value: Currency): string;
var
digits: string;
s: string;
floatRec: TFloatRec;
begin
FloatToDecimal({var}floatRec, Value, fvCurrency, 0{ignored for currency types}, 9999);
//convert the array of char into an easy to access string
digits := PChar(Addr(floatRec.Digits[0]));
if floatRec.Exponent > 0 then
begin
//Check for positive or negative infinity (exponent = 32767)
if floatRec.Exponent = 32767 then //David Heffernan says that currency can never be infinity. Even though i can't test it, i can at least try to handle it
begin
if floatRec.Negative = False then
Result := 'INF'
else
Result := '-INF';
Exit;
end;
{
digits: 1234567 89
exponent--------^ 7=7 digits on left of decimal mark
}
s := Copy(digits, 1, floatRec.Exponent);
{
for the value 10000:
digits: "1"
exponent: 5
Add enough zero's to digits to pad it out to exponent digits
}
if Length(s) < floatRec.Exponent then
s := s+StringOfChar('0', floatRec.Exponent-Length(s));
if Length(digits) > floatRec.Exponent then
s := s+'.'+Copy(digits, floatRec.Exponent+1, 20);
end
else if floatRec.Exponent < 0 then
begin
//check for NaN (Exponent = -32768)
if floatRec.Exponent = -32768 then //David Heffernan says that currency can never be NotANumber. Even though i can't test it, i can at least try to handle it
begin
Result := 'NAN';
Exit;
end;
{
digits: .000123456789
^---------exponent
}
//Add zero, or more, "0"'s to the left
s := '0.'+StringOfChar('0', -floatRec.Exponent)+digits;
end
else
begin
{
Exponent is zero.
digits: .123456789
^
}
if length(digits) > 0 then
s := '0.'+digits
else
s := '0';
end;
if floatRec.Negative then
s := '-'+s;
Result := s;
end;
除了 NAN
、INF
和 -INF
的边缘情况外,我现在可以将这些字符串传递给 Windows:
class function TGlobalization.GetCurrencyFormat(const DecimalString: WideString; const Locale: LCID): WideString;
var
cch: Integer;
ValueStr: WideString;
begin
Locale
LOCALE_INVARIANT
LOCALE_USER_DEFAULT <--- use this one (windows.pas)
LOCALE_SYSTEM_DEFAULT
LOCALE_CUSTOM_DEFAULT (Vista and later)
LOCALE_CUSTOM_UI_DEFAULT (Vista and later)
LOCALE_CUSTOM_UNSPECIFIED (Vista and later)
}
cch := Windows.GetCurrencyFormatW(Locale, 0, PWideChar(DecimalString), nil, nil, 0);
if cch = 0 then
RaiseLastWin32Error;
SetLength(ValueStr, cch);
cch := Windows.GetCurrencyFormatW(Locale, 0, PWideChar(DecimalString), nil, PWideChar(ValueStr), Length(ValueStr));
if (cch = 0) then
RaiseLastWin32Error;
SetLength(ValueStr, cch-1); //they include the null terminator /facepalm
Result := ValueStr;
end;
The
FloatToDecimalString
andGetNumberFormat
implementations are left as an exercise for the reader (since i actually haven't written the float one yet, just the currency - i don't know how i'm going to handle exponential notation).
还有鲍勃的你叔叔; Delphi 下正确本地化的 float 和货币。
我已经完成了正确本地化整数、日期、时间和日期时间的工作。
Note: Any code is released into the public domain. No attribution required.
关于string - 如何将 float 或货币转换为本地化字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7069204/
翻译 silverlight 5 应用程序 (Prism + MEF) 的可用选项是什么? 如果可能的话,我想: 没有 resx 文件(我不喜欢它们的管理方式) 而是提取并翻译 xaml 字符串(我认
我可以同时使用这两种方法来本地化 $| 还是我应该使用其中一种来支持另一种? 方法一:备份“_init_scr”中$|的旧值,并在调用“_end_win”时将$|设置回旧值。方式 2:调用 local
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C# localization , really confusing me 有人可以分享他们对大型 C# 应
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我正在使用 Phoenix 框架开发多语言应用程序 到目前为止,路由器看起来像这样: scope "/:locale", App do pipe_through [:browser, :bro
我想为我的域对象添加本地化支持。我有以下几点: class Person { int Id; City city; } class City { int Id; str
我需要 BlackBerry 本地化方面的帮助。我在 http://na.blackberry.com/eng/developers/resources/developer_labs.jsp#tab_
我正在尝试通过关注 documentation 来本地化我的 Flutter 应用程序. 我想要实现的是,在动态构建小部件的同时,我想转换来自我的模型的数据。这是我迄今为止尝试过的 List.gene
如何更改 EKEventEditViewController 中的默认语言,即使我手动设置 AppleLanguages 对象,它也始终是英语 最佳答案 我通过向我的应用程序 plist 添加一个 L
我在 iphone 日期选择器中遇到本地化问题。我希望日期选择器仅以英语显示日期,但现在它需要在 iphone 设置中设置为区域的语言。我尝试了各种没有用的方法像下面这样。 在 uidatepicke
我的应用仅支持荷兰语和法语。英语不是此应用程序的可用语言。如果我想使用可本地化的字符串,则默认值始终设置为英语。我希望这是荷兰语。所以我所做的就是使用英语可本地化字符串文件并用荷兰语单词填充它。我遇到
我即将本地化 iPhone 应用程序。当用户的语言(iOS 系统语言)是德语时,我想使用不同的 URL。 我想知道这是否是正确的方法: NSURL *url = [NSURL URLWithStrin
我正在将我的 iPhone 应用程序本地化为多种语言,除了更改一些字符串之外,我还需要更改一些背景。是否可以查询iPhone并获取用户的语言代码? 谢谢! 最佳答案 看一下 NSLocale: NSS
在本地化的 Iphone(语言设置为希伯来语)上,当我们使用 Safari 查看网页并点击输入字段时,我们会在键盘上显示希伯来语的“下一个/上一个/完成”按钮。 当我们使用应用程序中嵌入的 UIWeb
是否有更好的方法来存储大量文本以在 laravel 中进行本地化?如果我的整个页面只是纯文本,那会很容易,但是我的几个页面具有复杂的布局,我需要添加多个字符串来将文本包裹在图像/链接/媒体等内容周围。
我正在尝试将我的应用程序本地化。我为支持的语言创建了所需的 string.arb 文件。 为什么AppLocalizations.of(context)需要上下文? 我只是想访问文件/语言环境文件/类
绑定(bind)到 double可能会产生以下验证错误 Value ... could not be converted. 使用 ExceptionValidationRule 时错误更健谈: Inp
我有一些 Delphi 经验,并且正在尝试使用 Lazarus 构建一个项目,这对我来说是全新的。 我想,我已经阅读了有关 Lazarus、翻译/国际化/本地化的所有可用信息,但我无法找到我真正想要的
我一直在尝试更新网站的语言。 Controller public function getUpdateLanguage(Request $request) { $request_dat
我正在为我们正在启动的一个项目寻找框架和 CMS,该项目是一个需要支持本地化内容的网站。 Laravel 看起来不错,并且当然允许您本地化 UI 内容,但它似乎本身并不支持存储在数据库中的内容的本地化
我是一名优秀的程序员,十分优秀!