function JBRoundFloat(Value: Extended; const NumberOfDecimals: Byte = 2): Extended;
implementation
{ TAfrundning }
class function TAfrundning.Afrund(const Amount: Double; const CoinUnit: TCoinUnit): Double; begin Result := AfrundJB(Amount, CoinUnit).NewMount; end;
function TAfrundning.Afrund(const Amount: Double): Double; begin Result := Afrund(Amount, FCoinUnit) end;
class function TAfrundning.AfrundJB(const Amount: Double; const CoinUnit: TCoinUnit): TRoundStruct; begin Result.NewMount := JBRoundFloat(Amount / Integer(CoinUnit)) * Integer(CoinUnit); Result.Diff := JBRoundFloat(Result.NewMount - Amount); end;
constructor TAfrundning.Create(ACoinUnit: TCoinUnit); begin inherited Create; FCoinUnit := ACoinUnit; end;
function TAfrundning.FindRestVedAfrundning(const Amount: Double): Currency; begin Result := FindRestVedAfrundning(Amount, FCoinUnit) end;
constructor TAfrundning.Create; begin inherited; FCoinUnit := cu25; end;
class function TAfrundning.FindRestVedAfrundning(const Amount: Double; const CoinUnit: TCoinUnit): Currency; begin Result := AfrundJB(Amount, CoinUnit).Diff; end;
class function TAfrundning.JBRoundFloat(Value: Extended; const NumberOfDecimals: Byte): Extended; var Factor: Extended; begin Factor := IntPower(10, NumberOfDecimals);
if Frac(Value * Factor) >= 0.5 then Value := Value + 1 / (IntPower(10, NumberOfDecimals + 1));
Result := StrToFloat(Format('%.*f', [CurrencyDecimals, Value])); end;
function JBRoundFloat(Value: Extended; const NumberOfDecimals: Byte = 2): Extended; begin Result := TAfrundning.JBRoundFloat(Value, NumberOfDecimals); end;
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication4 { public struct RoundStruct { public double NewMount; public double Diff; }
Nu har jeg kun kigget på C# koden, men: - Math.Round brugt som i koden vil lave bankers rounding ikke 4/5 rounding som man bruger i Danmark - man bør ikke bruge double til beløb, man bør bruge decimal i.s.f.
if Frac(Value * Factor) >= 0.5 then Value := Value + 1 / (IntPower(10, NumberOfDecimals + 1)); Result := StrToFloat(Format('%.*f', [CurrencyDecimals, Value]));
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.