关于数据库中浮点运算、保留小数和时间计算

关于小数:

    1SQL数据表中使用numeric字段时(一定要注明小数位数),程序中使用:ADOQuery1.FieldByName('price').Value := RoundEx((ADOQuery1.FieldByName('price').AsFloat + strtofloat(Edit1.text)) * 1000) / 1000;或者ADOQuery1.FieldByName('price').Value:=adoquery1.FieldByName('price').AsFloat+strtofloat(edit1.Text);都可以保证数据计算的精度是准确值

2SQL数据表中使用float字段时,程序中使用ADOQuery1.FieldByName('price').Value := RoundEx((ADOQuery1.FieldByName('price').AsFloat + strtofloat(Edit1.text)) * 1000) / 1000;才可以保证精度;而使用:ADOQuery1.FieldByName('price').Value:=adoquery1.FieldByName('price').AsFloat+strtofloat(edit1.Text);就会出现浮点小数,使得计算结果不准确。

以上结果最多经过90000次循环测试。

关于次幂:

    Delphi中可以直接使用Power函数计算某个数的次幂,但是需要在Uses中加入uses math;函数用法:i:=Power(10,2);表示10的平方。

关于时间:

当要进行时间加减运算时,其计算结果是天为单位,如果需要换算成秒,则需要进行如下运算:

Edit6.Text:=floattostr(roundex((strtodatetime(edit5.Text)-strtodatetime(edit2.Text))*86400*100)/100);

注:(124小时,1小时60分钟,1分钟60秒,因此86400=24*60*60

原文地址:https://www.cnblogs.com/bingege/p/2147959.html