如何正确格式化字符串为德尔福的浮动数字? [英] How can I format string for float number on Delphi correctly?

查看:164
本文介绍了如何正确格式化字符串为德尔福的浮动数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c $   c> 1.0  - > 1或1.0
1.01 - > 1.01
1.001 - > 1.001
1.0000001 - > 1.0000001

有可能吗?标准格式字符串%0:f 只返回两个数字(例如,1.01)。 使用 格式rel =noreferrer> %g 通用格式字符串

  {$ APPTYPE CONSOLE} 

使用
System.SysUtils;

egin
Writeln(格式('%g',[1.0]));
Writeln(格式('%g',[1.01]));
Writeln(格式('%g',[1.001]));
Writeln(格式('%g',[1.0000001]));
结束。

输出

 
1
1.01
1.001
1.0000001


I need to call Format function to return the following strings:

1.0 -> "1 or 1.0"
1.01 - > "1.01"
1.001 - > "1.001"
1.0000001 -> "1.0000001"

It it possible? The standard format string %0:f returns only two digits after the point (for example, "1.01").

解决方案

Use the %g general format string:

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

begin
  Writeln(Format('%g', [1.0]));
  Writeln(Format('%g', [1.01]));
  Writeln(Format('%g', [1.001]));
  Writeln(Format('%g', [1.0000001]));
end.

Output

1
1.01
1.001
1.0000001

这篇关于如何正确格式化字符串为德尔福的浮动数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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