单精度格式化函数

  1 //1.定义变量
  2 Temp_F: real = 113.05;
  3
  4 //2.执行函数
  5 procedure TForm1.Button1Click(Sender: TObject);
  6 begin
  7   Edit1.text :=FormatFloat(‘0.0‘,Temp_F);
  8 end;
  9
 10 procedure TForm1.Button2Click(Sender: TObject);
 11 begin
 12   Edit2.text :=FormatCurr(‘0.0‘,Temp_F);
 13 end;
 14
 15 procedure TForm1.Button3Click(Sender: TObject);
 16   function RoundFloat(f:double;i:integer):double;
 17   var
 18     s:string;
 19     ef:extended;
 20   begin
 21     s:=‘#.‘+StringOfChar(‘0‘,i);
 22     ef:=StrToFloat(FloatToStr(f));//防止浮点运算的误差
 23     result:=StrToFloat(FormatFloat(s,ef));
 24   end;
 25 begin
 26   Edit3.text :=FloattoStr(RoundFloat(Temp_F,2));
 27 end;
 28
 29 procedure TForm1.Button4Click(Sender: TObject);
 30   function MyRound(s:real;non:integer):real;
 31   var
 32     roundi:integer;
 33     j:double;
 34   begin
 35     j:=s;
 36     for roundi:=10 downto non do
 37     begin
 38      j:=j+1/power(10,roundi+2);
 39      j:=roundto(j,-roundi);
 40     end;
 41     result:=j;
 42   end;
 43 begin
 44   Edit4.text :=FloattoStr(MyRound(Temp_F,3));
 45 end;
 46
 47 procedure TForm1.Button5Click(Sender: TObject);
 48   Function FRoundInt64(x:Extended):Int64;
 49   var
 50     Temp:Extended;
 51   begin
 52     Temp:=Frac(x);
 53     if temp=0.5 then
 54       Result:=Trunc(x)+1
 55     else
 56       Result:=Trunc(x);
 57   end;
 58 begin
 59   Edit5.text :=FloattoStr(FRoundInt64(Temp_F));
 60 end;
 61
 62 procedure TForm1.Button6Click(Sender: TObject);
 63   function DoRound(Value: Extended): Int64;
 64   procedure Set8087CW(NewCW: Word);
 65   asm
 66     MOV     Default8087CW,AX
 67     FNCLEX
 68     FLDCW   Default8087CW
 69   end;
 70   const
 71     RoundUpCW         = $1B32;
 72   var
 73     OldCW             : Word;
 74   begin
 75     OldCW := Default8087CW;
 76     try
 77       Set8087CW(RoundUpCW);
 78       Result := Round(Value);
 79     finally
 80       Set8087CW(OldCW);
 81     end;
 82   end;
 83 begin
 84   Edit6.text :=FloattoStr(DoRound(Temp_F));
 85 end;
 86
 87 procedure TForm1.Button7Click(Sender: TObject);
 88   const
 89     defDoubleEpsilon      = 1E-12;
 90   function FRound(F: Double; ADecimal: Integer; AEpsilon: Double = defDoubleEpsilon): Double;
 91   const
 92     CDecBase: array[0..9] of Double = (
 93       1, 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9);
 94   var
 95     P: Int64 absolute F;
 96     IntVal, DecimalVal, ModVal: Int64;
 97   begin
 98     if ADecimal < 0 then
 99     begin
100       IntVal := Trunc(F);
101       ADecimal := Abs(ADecimal);
102       if ADecimal > 9 then
103         raise Exception.CreateFmt(‘Not Support Param -%d.‘, [ADecimal]);
104       IntVal := IntVal div Trunc(CDecBase[ADecimal - 1]);
105       ModVal := IntVal mod 10;
106       IntVal := IntVal div 10;
107       if ModVal >= 5 then
108         Inc(IntVal, 1)
109       else if ModVal <= -5 then
110         Inc(IntVal, -1);
111       Result := IntVal * CDecBase[ADecimal];
112     end
113     else if ADecimal <= 8 then
114     begin
115       Inc(P, 512); // 可保留14位有效数字(13位准确,最后一位可能差1)
116       IntVal := Trunc(F);
117       DecimalVal := Trunc(Frac(F) * CDecBase[ADecimal + 1]);
118       ModVal := DecimalVal mod 10;
119       if ModVal >= 5 then
120         Inc(DecimalVal, 10)
121       else if ModVal <= -5 then
122         Inc(DecimalVal, -10);
123       Result := IntVal + (DecimalVal div 10) / CDecBase[ADecimal];
124     end
125     else begin
126       Result := StrToFloat(FormatFloat(‘0.‘ + StringOfChar(‘#‘, ADecimal), F + AEpsilon));
127       if SameValue(Result, 0.0) then Result := 0;
128     end;
129   end;
130 begin
131   Edit7.text :=FloattoStr(FRound(Temp_F,3));
132 end; 
时间: 2025-01-12 11:33:33

单精度格式化函数的相关文章

sql常用格式化函数及字符串函数

一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') //2017-09-18 22:41:50 YYYY:年(4和更多位) MM:月份号(01-12) DD:一个月里的日(01-31) HH24:一天的小时数(00-23) MI:分钟(00-59) SS:秒(00-59) 2.字符串转日期 select to_date('2017-09-18','YYYY-MM-DD') //2017-09-

delphi Format格式化函数

Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const Format: string; const Args: array of const): string; overload; 事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的, 但并不多用,所以这里只对第一个介绍:function Format(const Format: st

js时间格式化函数,支持Unix时间戳

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"> <title>js时间格式化函数,支持Unix时间戳</title> </head>

js 日期格式化函数

直接上代码: // 日期格式化函数 // yyyy/MM/dd hh:mm:ss SSS ⇒ "2017/05/16 09:24:20 850" //"yyyy/M/d h:m:s SSS"⇒ "2017/5/16 9:24:35 723" Date.prototype.format2 = function(format) { var map = { 'M+': this.getMonth() + 1, 'd+': this.getDate(),

js 日期格式化函数(可自定义)

js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str = formatStr; var Week = ['日', '一', '二', '三', '四', '五', '六']; str = str.replace(/yyyy|YYYY/, dat.getFullYear()); str = str.replace(/yy|YY/, (dat.getYea

js时间戳格式化函数

/** * 格式化日期函数 * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 * (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 */Date.prototype.format = function(format){ var o = { "M+" : this.

5个缺失的 JavaScript 数字格式化函数

/** 下面两个函数都能对浮点数进行四舍五入,保留小数点后两位 **/ function CurrencyFormatted(amount) { var i = parseFloat(amount); if(isNaN(i)) { i = 0.00; } var minus = ''; if(i < 0) { minus = '-'; } i = Math.abs(i); i = parseInt((i + .005) * 100); i = i / 100; s = new String(i)

PHP中常用的字符串格式化函数总结

注意:在PHP中提供的字符串函数处理的字符串,大部分都不是在原字符串上修改,而是返回一个格式化后的新字符串. 一.取出空格和字符串填补函数 空格也是一个有效的字符,在字符串中也会占据一个位置.用户在表单输入数据时,经常在无意中会多输入一些无意义的空格.因此PHP脚本在接收到通过表单处理过来的数据时,首先处理的就是字符串中多余的空格,或者其他一些没有意义的符号.在PHP中可以通过ltrim().rtrim()和trim()函数来完成这项工作.这三个函数的语法格式相同,但作用有所不同.他们的语法格式

SQL笔记 GETDATE()日期格式化函数

Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE(), 1): 05/16/06Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06Select