@@ -510,6 +510,19 @@ BigDecimal = record
510
510
// / <para>Only the low 8 bits of myUInt64 are copied to the byte.</para></remarks>
511
511
class operator Explicit(const Value : BigDecimal): Int64;
512
512
513
+ // -- Conversion functions --
514
+
515
+ { $IFDEF HasExtended}
516
+ function AsExtended : Extended;
517
+ { $ENDIF}
518
+ function AsDouble : Double;
519
+ function AsSingle : Single;
520
+ function AsBigInteger : BigInteger;
521
+ function AsUInt64 : UInt64;
522
+ function AsInt64 : Int64;
523
+ function AsUInt32 : UInt32;
524
+ function AsInt32 : Int32;
525
+
513
526
514
527
// -- Mathematical functions --
515
528
@@ -942,6 +955,82 @@ class procedure BigDecimal.AdjustForRoundingMode(var Quotient: BigInteger; const
942
955
end ;
943
956
end ;
944
957
958
+ { $IFDEF HasExtended}
959
+ function BigDecimal.AsExtended : Extended;
960
+ begin
961
+
962
+ end ;
963
+ { $ENDIF}
964
+
965
+ function BigDecimal.AsDouble : Double;
966
+ begin
967
+ Result := Double(Self);
968
+ if IsInfinite(Result) then
969
+ Error(ecConversion, [' BigDecimal' , ' Double' ]);
970
+ end ;
971
+
972
+ function BigDecimal.AsSingle : Single;
973
+ begin
974
+ Result := Single(Self);
975
+ if IsInfinite(Result) then
976
+ Error(ecConversion, [' BigDecimal' , ' Single' ]);
977
+ end ;
978
+
979
+ function BigDecimal.AsBigInteger : BigInteger;
980
+ begin
981
+ Result := BigInteger(Self);
982
+ if Self.Scale > 0 then
983
+ Error(ecRounding, []);
984
+ end ;
985
+
986
+ function BigDecimal.AsUInt64 : UInt64;
987
+ var
988
+ D: BigDecimal;
989
+ begin
990
+ D := Self.RoundToScale(0 , rmUnnecessary); // Throws if rounding necessary
991
+ try
992
+ Result := D.UnscaledValue.AsUInt64; // Throws if too big
993
+ except
994
+ Error(ecConversion, [' BigDecimal' , ' UInt64' ]);
995
+ end ;
996
+ end ;
997
+
998
+ function BigDecimal.AsInt64 : Int64;
999
+ var
1000
+ D: BigDecimal;
1001
+ begin
1002
+ D := Self.RoundToScale(0 , rmUnnecessary);
1003
+ try
1004
+ Result := D.UnscaledValue.AsInt64;
1005
+ finally
1006
+ Error(ecConversion, [' BigDecimal' , ' Int64' ]);
1007
+ end ;
1008
+ end ;
1009
+
1010
+ function BigDecimal.AsUInt32 : UInt32;
1011
+ var
1012
+ D: BigDecimal;
1013
+ begin
1014
+ D := Self.RoundToScale(0 , rmUnnecessary);
1015
+ try
1016
+ Result := D.UnscaledValue.AsCardinal;
1017
+ finally
1018
+ Error(ecConversion, [' BigDecimal' , ' UInt32' ]);
1019
+ end ;
1020
+ end ;
1021
+
1022
+ function BigDecimal.AsInt32 : Int32;
1023
+ var
1024
+ D: BigDecimal;
1025
+ begin
1026
+ D := Self.RoundToScale(0 , rmUnnecessary);
1027
+ try
1028
+ Result := D.UnscaledValue.AsInteger;
1029
+ finally
1030
+ Error(ecConversion, [' BigDecimal' , ' Int32' ]);
1031
+ end ;
1032
+ end ;
1033
+
945
1034
// Does a "binary search" to remove trailing zeros. This is much faster (10 times or more) than repeatedly
946
1035
// dividing by BigInteger.Ten until there is a remainder, even for relatively small numbers of trailing zeros.
947
1036
// Since this modifies Value, it cannot be made public, but there is a public version of this, called
0 commit comments