remove from float
This commit is contained in:
parent
ac9b4bfd0e
commit
9f07ab8d4a
@ -147,8 +147,10 @@ export class Amounts {
|
|||||||
throw Error(`incompatible currency (${am1.currency} vs${am2.currency})`);
|
throw Error(`incompatible currency (${am1.currency} vs${am2.currency})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const x1 = BigInt(am1.value) * BigInt(amountFractionalBase) + BigInt(am1.fraction);
|
const x1 =
|
||||||
const x2 = BigInt(am2.value) * BigInt(amountFractionalBase) + BigInt(am2.fraction);
|
BigInt(am1.value) * BigInt(amountFractionalBase) + BigInt(am1.fraction);
|
||||||
|
const x2 =
|
||||||
|
BigInt(am2.value) * BigInt(amountFractionalBase) + BigInt(am2.fraction);
|
||||||
|
|
||||||
const quotient = x1 / x2;
|
const quotient = x1 / x2;
|
||||||
const remainderScaled = x1 % x2;
|
const remainderScaled = x1 % x2;
|
||||||
@ -158,9 +160,9 @@ export class Amounts {
|
|||||||
remainder: {
|
remainder: {
|
||||||
currency: am1.currency,
|
currency: am1.currency,
|
||||||
value: Number(remainderScaled / BigInt(amountFractionalBase)),
|
value: Number(remainderScaled / BigInt(amountFractionalBase)),
|
||||||
fraction: Number(remainderScaled % BigInt(amountFractionalBase))
|
fraction: Number(remainderScaled % BigInt(amountFractionalBase)),
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static sum(amounts: AmountLike[]): Result {
|
static sum(amounts: AmountLike[]): Result {
|
||||||
@ -391,20 +393,6 @@ export class Amounts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a float to a Taler amount.
|
|
||||||
* Loss of precision possible.
|
|
||||||
*/
|
|
||||||
static fromFloat(floatVal: number, currency: string): AmountJson {
|
|
||||||
return {
|
|
||||||
currency,
|
|
||||||
fraction: Math.floor(
|
|
||||||
(floatVal - Math.floor(floatVal)) * amountFractionalBase,
|
|
||||||
),
|
|
||||||
value: Math.floor(floatVal),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static min(a: AmountLike, b: AmountLike): AmountJson {
|
static min(a: AmountLike, b: AmountLike): AmountJson {
|
||||||
const cr = Amounts.cmp(a, b);
|
const cr = Amounts.cmp(a, b);
|
||||||
if (cr >= 0) {
|
if (cr >= 0) {
|
||||||
|
@ -187,6 +187,31 @@ interface GetPlanForWalletInitiatedOperation {
|
|||||||
mode: TransactionAmountMode;
|
mode: TransactionAmountMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ConvertAmountRequest {
|
||||||
|
amount: AmountString;
|
||||||
|
type: TransactionAmountMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const codecForConvertAmountRequest =
|
||||||
|
buildCodecForObject<ConvertAmountRequest>()
|
||||||
|
.property("amount", codecForAmountString())
|
||||||
|
.property(
|
||||||
|
"type",
|
||||||
|
codecForEither(
|
||||||
|
codecForConstString(TransactionAmountMode.Raw),
|
||||||
|
codecForConstString(TransactionAmountMode.Effective),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.build("ConvertAmountRequest");
|
||||||
|
|
||||||
|
export interface GetAmountRequest {
|
||||||
|
currency: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const codecForGetAmountRequest = buildCodecForObject<GetAmountRequest>()
|
||||||
|
.property("currency", codecForString())
|
||||||
|
.build("GetAmountRequest");
|
||||||
|
|
||||||
interface GetPlanToCompleteOperation {
|
interface GetPlanToCompleteOperation {
|
||||||
instructedAmount: AmountString;
|
instructedAmount: AmountString;
|
||||||
}
|
}
|
||||||
@ -328,6 +353,17 @@ export const codecForGetPlanForOperationResponse =
|
|||||||
.property("counterPartyAmount", codecOptional(codecForAmountString()))
|
.property("counterPartyAmount", codecOptional(codecForAmountString()))
|
||||||
.build("GetPlanForOperationResponse");
|
.build("GetPlanForOperationResponse");
|
||||||
|
|
||||||
|
export interface AmountResponse {
|
||||||
|
effectiveAmount: AmountString;
|
||||||
|
rawAmount: AmountString;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const codecForAmountResponse = (): Codec<AmountResponse> =>
|
||||||
|
buildCodecForObject<AmountResponse>()
|
||||||
|
.property("effectiveAmount", codecForAmountString())
|
||||||
|
.property("rawAmount", codecForAmountString())
|
||||||
|
.build("AmountResponse");
|
||||||
|
|
||||||
export interface Balance {
|
export interface Balance {
|
||||||
scopeInfo: ScopeInfo;
|
scopeInfo: ScopeInfo;
|
||||||
available: AmountString;
|
available: AmountString;
|
||||||
|
Loading…
Reference in New Issue
Block a user