四舍五入货币 [英] rounding a currency

查看:127
本文介绍了四舍五入货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码对货币进行四舍五入

function MyRound(value :currency) : integer;

begin
  if value > 0 then
    result := Trunc(value + 0.5)
  else
    result := Trunc(value - 0.5);
end;

到目前为止,它运行良好,现在的问题是,如果我想对 999999989000.40 这样的货币进行四舍五入,因为Truc采用int,而MyRound也返回int,所以它给出负值.

我可能的解决方案是将货币转换为字符串,并在之前获取字符串.,然后将字符串转换回货币.这是正确的方法吗?我是delpi的新手,请帮帮我.

解决方案

您正在使事情变得过于复杂.您可以简单地使用Round:

program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  C: Currency;

begin
  C := 999999989000.4;
  Writeln(Round(C));
  C := 999999989000.5;
  Writeln(Round(C));
  C := 999999989000.6;
  Writeln(Round(C));
  C := 999999989001.4;
  Writeln(Round(C));
  C := 999999989001.5;
  Writeln(Round(C));
  C := 999999989001.6;
  Writeln(Round(C));
  Readln;
end.

输出

999999989000
999999989000
999999989001
999999989001
999999989002
999999989002

如果您不希望银行家四舍五入,并且确实希望使用Trunc逻辑,那么您确实需要编写自己的函数.但是您的函数存在的问题是它被截断为32位整数.使函数返回64位整数:

program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  C: Currency;

function MyRound(const Value: Currency): Int64;
begin
  if Value > 0 then
    result := Trunc(Value + 0.5)
  else
    result := Trunc(Value - 0.5);
end;

begin
  C := 999999989000.4;
  Writeln(MyRound(C));
  C := 999999989000.5;
  Writeln(MyRound(C));
  C := 999999989000.6;
  Writeln(MyRound(C));
  C := 999999989001.4;
  Writeln(MyRound(C));
  C := 999999989001.5;
  Writeln(MyRound(C));
  C := 999999989001.6;
  Writeln(MyRound(C));
  Readln;
end.

999999989000
999999989001
999999989001
999999989001
999999989002
999999989002

i have the following code to round the currency

function MyRound(value :currency) : integer;

begin
  if value > 0 then
    result := Trunc(value + 0.5)
  else
    result := Trunc(value - 0.5);
end;

it worked well so far, my problem now is if i want to round a currency like 999999989000.40 it is giving negative value since Truc takes int and MyRound also returns int.

My possible solutions is to convert currency to string and get the string before . and convert the string back to currency. Is this a right approach? i am new to delpi so pls help me out.

解决方案

You are overcomplicating matters. You can simply use Round:

program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  C: Currency;

begin
  C := 999999989000.4;
  Writeln(Round(C));
  C := 999999989000.5;
  Writeln(Round(C));
  C := 999999989000.6;
  Writeln(Round(C));
  C := 999999989001.4;
  Writeln(Round(C));
  C := 999999989001.5;
  Writeln(Round(C));
  C := 999999989001.6;
  Writeln(Round(C));
  Readln;
end.

which outputs

999999989000
999999989000
999999989001
999999989001
999999989002
999999989002

If you don't want banker's rounding, and you really do want your Trunc logic then you do need to write your own function. But the problem with your function is that it was truncating to 32 bit integer. Make the function return a 64 bit integer:

program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  C: Currency;

function MyRound(const Value: Currency): Int64;
begin
  if Value > 0 then
    result := Trunc(Value + 0.5)
  else
    result := Trunc(Value - 0.5);
end;

begin
  C := 999999989000.4;
  Writeln(MyRound(C));
  C := 999999989000.5;
  Writeln(MyRound(C));
  C := 999999989000.6;
  Writeln(MyRound(C));
  C := 999999989001.4;
  Writeln(MyRound(C));
  C := 999999989001.5;
  Writeln(MyRound(C));
  C := 999999989001.6;
  Writeln(MyRound(C));
  Readln;
end.

999999989000
999999989001
999999989001
999999989001
999999989002
999999989002

这篇关于四舍五入货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆