BCD operations now support BigNumbers

This commit is contained in:
n1474335 2018-01-05 18:38:23 +00:00
parent 283d3e1e7b
commit 53eba2337c
2 changed files with 9 additions and 8 deletions

View File

@ -3750,7 +3750,7 @@ const OperationConfig = {
module: "Default", module: "Default",
description: "Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four or eight. Special bit patterns are sometimes used for a sign.", description: "Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four or eight. Special bit patterns are sometimes used for a sign.",
inputType: "string", inputType: "string",
outputType: "number", outputType: "BigNumber",
args: [ args: [
{ {
name: "Scheme", name: "Scheme",
@ -3778,7 +3778,7 @@ const OperationConfig = {
"To BCD": { "To BCD": {
module: "Default", module: "Default",
description: "Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four or eight. Special bit patterns are sometimes used for a sign", description: "Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four or eight. Special bit patterns are sometimes used for a sign",
inputType: "number", inputType: "BigNumber",
outputType: "string", outputType: "string",
args: [ args: [
{ {

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js"; import Utils from "../Utils.js";
import BigNumber from "bignumber.js";
/** /**
@ -61,14 +62,14 @@ const BCD = {
/** /**
* To BCD operation. * To BCD operation.
* *
* @param {number} input * @param {BigNumber} input
* @param {Object[]} args * @param {Object[]} args
* @returns {string} * @returns {string}
*/ */
runToBCD: function(input, args) { runToBCD: function(input, args) {
if (isNaN(input)) if (input.isNaN())
return "Invalid input"; return "Invalid input";
if (Math.floor(input) !== input) if (!input.floor().equals(input))
return "Fractional values are not supported by BCD"; return "Fractional values are not supported by BCD";
const encoding = BCD.ENCODING_LOOKUP[args[0]], const encoding = BCD.ENCODING_LOOKUP[args[0]],
@ -77,7 +78,7 @@ const BCD = {
outputFormat = args[3]; outputFormat = args[3];
// Split input number up into separate digits // Split input number up into separate digits
const digits = input.toString().split(""); const digits = input.toFixed().split("");
if (digits[0] === "-" || digits[0] === "+") { if (digits[0] === "-" || digits[0] === "+") {
digits.shift(); digits.shift();
@ -152,7 +153,7 @@ const BCD = {
* *
* @param {string} input * @param {string} input
* @param {Object[]} args * @param {Object[]} args
* @returns {number} * @returns {BigNumber}
*/ */
runFromBCD: function(input, args) { runFromBCD: function(input, args) {
const encoding = BCD.ENCODING_LOOKUP[args[0]], const encoding = BCD.ENCODING_LOOKUP[args[0]],
@ -206,7 +207,7 @@ const BCD = {
output += val.toString(); output += val.toString();
}); });
return parseInt(output, 10); return new BigNumber(output);
}, },
}; };