如何在jolt中获得两位小数?

huangapple go评论63阅读模式
英文:

How to get the two decimal places in jolt?

问题

"Scenarios" in Chinese is "情景"。

  1. 当价格字段为7.8时,将返回7.8,而不是7.80。
  2. 当价格字段为0时,将返回0.0,而不是0.00。
  3. 当价格字段为1时,将返回1.0,而不是1.00。

我的问题是,如何可以在所有上述情景中使数字保留两位小数?

英文:

We have input JSON is like below

Input JSON

[
  {
    "price": 120,
    "quantity": 1
  },
  {
    "price": 7.88,
    "quantity": 1
  },
  {
    "price": 7.8,
    "quantity": 1
  },
  {
    "price": 12.849,
    "quantity": 1
  },
  {
    "price": 0,
    "quantity": 1
  }
]

From the above input JSON we want to perform some calculation like price * quantity

NOTE: And the number should have up to two decimal places

Jolt Spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "inverseQuantity": "=divide(1,@(1,quantity))",
        "orderTotal": ["=divide(@(1,price),@(1,inverseQuantity))", 0],
        "orderTotalRounded": "=divideAndRound(2,@(1,orderTotal),1)"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "inverseQuantity": "",
        "orderTotal": ""
      }
    }
  }
]

However, we did not receive the expected output from the above Jolt spec. Since we want the decimal up to two places.

Output JSON

[
  {
    "price": 120,
    "quantity": 1,
    "orderTotalRounded": 120
  },
  {
    "price": 7.88,
    "quantity": 1,
    "orderTotalRounded": 7.88
  },
  {
    "price": 7.8,
    "quantity": 1,
    "orderTotalRounded": 7.8
  },
  {
    "price": 12.849,
    "quantity": 1,
    "orderTotalRounded": 12.85
  },
  {
    "price": 0,
    "quantity": 1,
    "orderTotalRounded": 0
  }
]

Scenarios

  1. When the price field has 7.8 then it will return 7.8 instead of 7.80
  2. When the price field has 0 then it will return 0.0 instead of 0.00
  3. When the price field has 1 then it will return 1.0 instead of 1.00

My question is, how can we get the numbers up to two decimal places for all the above cases?

答案1

得分: 2

Jolt Spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "del_inverseQuantity": "=divide(1,@(1,quantity))",
        "del_orderTotal": ["=divideAndRound(2,@(1,price),@(1,del_inverseQuantity))", 0],
        "del_orderTotalString": "=toString(@(1,del_orderTotal))",
        "del_orderTotalSplited": "=split('.',@(1,del_orderTotalString))",
        "del_orderTotalPadded": "=rightPad(@(1,del_orderTotalSplited[1]), 2, '0')",
        "orderTotalConcat": "=concat(@(1,del_orderTotalSplited[0]), '.', @(1,del_orderTotalPadded))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "del_*": ""
      }
    }
  }
]

Output JSON

[
  {
    "price": 120,
    "quantity": 1,
    "orderTotalConcat": "120.00"
  },
  {
    "price": 7.88,
    "quantity": 1,
    "orderTotalConcat": "7.88"
  },
  {
    "price": 7.8,
    "quantity": 1,
    "orderTotalConcat": "7.80"
  },
  {
    "price": 12.849,
    "quantity": 1,
    "orderTotalConcat": "12.85"
  },
  {
    "price": 0,
    "quantity": 1,
    "orderTotalConcat": "0.00"
  }
]

你的翻译要求已完成。

英文:

I have prepared the solution, below is the jolt spec which I have tried and got the desired output

Jolt Spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "del_inverseQuantity": "=divide(1,@(1,quantity))",
        "del_orderTotal": ["=divideAndRound(2,@(1,price),@(1,del_inverseQuantity))", 0],
        "del_orderTotalString": "=toString(@(1,del_orderTotal))",
        "del_orderTotalSplited": "=split('\\.',@(1,del_orderTotalString))",
        "del_orderTotalPadded": "=rightPad(@(1,del_orderTotalSplited[1]), 2, '0')",
        "orderTotalConcat": "=concat(@(1,del_orderTotalSplited[0]), '.', @(1,del_orderTotalPadded))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "del_*": ""
      }
    }
  }
]

Output JSON

[
  {
    "price": 120,
    "quantity": 1,
    "orderTotalConcat": "120.00"
  },
  {
    "price": 7.88,
    "quantity": 1,
    "orderTotalConcat": "7.88"
  },
  {
    "price": 7.8,
    "quantity": 1,
    "orderTotalConcat": "7.80"
  },
  {
    "price": 12.849,
    "quantity": 1,
    "orderTotalConcat": "12.85"
  },
  {
    "price": 0,
    "quantity": 1,
    "orderTotalConcat": "0.00"
  }
]

I want to know if this is the correct way or if I should explore other ways.

huangapple
  • 本文由 发表于 2023年3月15日 21:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定