Lazarus 四舍五入与Simple Program显示中文方法

这段代码展示了在Delphi中进行四舍五入和取整的各种方法,包括Trunc、Round、Ceil和Floor函数的用法。此外,还演示了设置不同舍入模式对数值的影响,如银行家舍入。最后,代码演示了在不同舍入模式下处理负数和正数的情况。
摘要由CSDN通过智能技术生成

program roundingtointeger;
{$codepage UTF8}
uses math;
var
  some_int:real;
  some_real:real;
  I,Count,guess : Longint;
  R : Real;
begin
  some_int :=375.5;
  some_real:=375.4;
  writeln('PI=',pi);
  writeln('Some_int =' ,trunc(some_int));
  writeln('some_int=',round(some_int)) ;
  writeln('some_real=',trunc(some_real));
  writeln('some_real',round(some_real));

  writeln('Ceil');
  Writeln(Ceil(-3.7)); // should be -3
  Writeln(Ceil(3.7));  // should be 4
  Writeln(Ceil(-4.0)); // should be -4

  writeln('floor');
  Writeln(floor(-3.7)); // should be -4
  Writeln(floor(3.7));  // should be 3
  Writeln(floor(-4.0)); // should be -4

  writeln('Round');
  WriteLn( Round(8.7) );//9 
  WriteLn( Round(8.5) );//8
  WriteLn( Round(8.3) );//8  
  WriteLn( Round(7.5) );//8
  WriteLn( Round(5.4) );//5
  WriteLn( Round(5.5) );//6 
  WriteLn( Round(5.6) );//6
  // examples of "bankers rounding" - .5 is adjusted to the nearest even number
  WriteLn( Round(2.5) ); //2
  WriteLn( Round(3.5) );//4

  writeln('pred or succ of c');
  writeln('succ=',succ('c'));
  writeln('pred=',pred('c'));   
  writeln('pred or succ of 0');
  writeln('succ=',succ(0));
  writeln('pred=',pred(0));   
  writeln('pred or succ of "0"');
  writeln('succ=',succ('0'));
  writeln('pred=',pred('0'));


  writeln('设置四舍五入模式');
  writeln('Round to',roundto(2.5,0));
  setroundmode(rmNearest);
  writeln('2.5 Nearest=',roundto(2.5,0));
  setroundmode(rmDown);
  writeln('2.5 roundDown=',roundto(2.5,0));
  setroundmode(rmUp);
  writeln('2.5 roundUP=',roundto(2.5,0));
  setroundmode(rmTruncate);
  writeln('2.5 roundTruncate=',roundto(2.5,0));

  //正常的四舍五入,请使用Math.SetRoundMode修改
  Math.SetRoundMode(rmUp);
  write(trunc(roundto(-2.5,0)),' ');
  write(trunc(roundto(-1.5,0)),' ');
  write(trunc(roundto(-0.5,0)),' ');
  write(roundto(0.5,0),' ');
  write(trunc(roundto(0.5,0)),' ');
  write(trunc(roundto(1.5,0)),' ');
  writeln(trunc(roundto(2.5,0)));

  readln();
end.
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值