注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

Delphi double to string: FloatToStrF & FloatToStr  

2016-03-29 19:19:27|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Convert a floating point value to a string with formattingSysUtils unit
1 function FloatToStrF ( Value : Extended; Format : TFloatFormat; Precision, Digits : Integer ) : string;
2 function FloatToStrF ( Value : Extended; Format : TFloatFormat; Precision, Digits : Integer; const?FormatSettings : TFormatSettings ) : string;
Description
The FloatToStrF function converts a floating point number Value into a displayable string, with great control over the formatting via the Format, Precision, and Digits values. 
 
The Value type may be any of the floating point types. 
 
The Format parameter is defined by theTFloatFormat (SysUtils) type : 
 
ffCurrency eg : ?2,345.60
ffExponent eg : 2.3456E+04
ffFixed eg : 2345.60
ffGeneral eg : 2345.6
ffNumber eg : 2,345.6

 
The other parameters are dependent on this format. See TFloatFormat for full details. 
 
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.
Notes
You can change the currency string from the default (such as '$' in the USA, '?' in the UK) using theCurrencyString variable.

You can change the position of the currency string using the CurrencyFormat variable.

You can change the decimal point value by setting theDecimalSeparator character.

You can change the thousands separator value by setting the ThousandSeparator character.

If the full number of digits before the decimal point (the mantissa) cannot be displayed, then the display reverts to the exponent (scientific) format.


Example code : Display numbers as financial values
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in a Currency format
  CurrencyString := '? ';
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffCurrency, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffCurrency, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffCurrency, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffCurrency, 2, 4));
end;
 
end.

   Using 8,4 = ? 1,234.5670
   Using 4,4 = ? 1,235.0000
   Using 4,2 = ? 1,235.00
   Using 2,4 = 1.2E0003
 
Example code : Display numbers with Scientific formatting
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in a Scientific format
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffExponent, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffExponent, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffExponent, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffExponent, 2, 4));
end;
 
end.

   Using 8,4 = 1.2345670E+0003
   Using 4,4 = 1.235E+0003
   Using 4,2 = 1.235E+03
   Using 2,4 = 1.2E+0003
 
Example code : General display of numbers
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in Fixed format
  ShowMessage('Fixed formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffFixed, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffFixed, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffFixed, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffFixed, 2, 4));

  // Display in General format
  ShowMessage('');
  ShowMessage('General formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffGeneral, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffgeneral, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffGeneral, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffGeneral, 2, 4));

  // Display in Number format
  ShowMessage('');
  ShowMessage('Number formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffNumber, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffNumber, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffNumber, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffNumber, 2, 4));
end;
 
end.

   Fixed formatting :
   
   Using 8,4 = 1234.5670
   Using 4,4 = 1235.0000
   Using 4,2 = 1235.00
   Using 2,4 = 1.2E0003
   
   General formatting :
   
   Using 8,4 = 1234.567
   Using 4,4 = 1235
   Using 4,2 = 1235
   Using 2,4 = 1.2E0003
   
   Number formatting :
   
   Using 8,4 = 1,234.5670
   Using 4,4 = 1,235.0000
   Using 4,2 = 1,235.00
   Using 2,4 = 1.2E0003

----------------------------------------------------------------------------------------------------------------------------------

Example code : Display various sizes of extended data values
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStr command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1, amount2, amount3 : Extended;
begin
  amount1 := 1234567890.123456789;  // High precision number
  amount2 := 1234567890123456.123;  // High mantissa digits
  amount3 := 1E100;                 // High value number

  ShowMessage('Amount1 = '+FloatToStr(amount1));
  ShowMessage('Amount2 = '+FloatToStr(amount2));
  ShowMessage('Amount3 = '+FloatToStr(amount3));
end;
 
end.

   Amount1 = 1234567890.12346
   Amount2 = 1.23456789012346E15
   Amount3 = 1E100
  评论这张
 
阅读(241)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016