delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
roundto函数带来的问题 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiAPI/html/delphi_20061120094828197.html
single类型的四字节浮点数,用roundto(asingle,-2)保留两位小数,这时出现个一个问题,是如果asingle小于或大于一个值的时候,asingle就用科学计数法表示,在用roundto就会有错误,现在想知道这个界限值,怎么判断这个值避免这个问题

能不能避免用科学计数法表示

最好的办法是用FloatToStr,然后再对字符串操作,精度上不会出现xx.xx9999999999这样的情况。

学习;

对付这类问题,比较灵活的办法是自己编程处理。你可以看看roundto的源码  
   
  function   RoundTo(const   AValue:   Double;   const   ADigit:   TRoundToRange):   Double;  
  var  
      LFactor:   Double;  
  begin  
      LFactor   :=   IntPower(10,   ADigit);  
      Result   :=   Round(AValue   /   LFactor)   *   LFactor;  
  end;  
   
  其中intpower   原型:  
  function   intpower(base:float;const   exponent:Integer):float    
  功能:返回base的exponent次方  
   
 

今天起得早,运动完后做下数学题,由于没有相关准确资料,   我用硬代码测试了一下,这个临界值=92233722601930753。(aSingle   >=   临界值则roundto(asingle,-2)出现异常).   希望是正确的。

如果带小数点的话,   临界值   =   92233722601930752.005

roundto   建议少用  
   
  还是用FloatToStr   好用点

我写了个1E15,不管精确度了

1E15小了一些。


文章来源:http://www.delphi2007.net/DelphiAPI/html/delphi_20061120094828197.html
posted on 2008-11-27 21:11 delphi2007 阅读(198) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。