英文:
How to get the two decimal places in jolt?
问题
"Scenarios" in Chinese is "情景"。
- 当价格字段为7.8时,将返回7.8,而不是7.80。
- 当价格字段为0时,将返回0.0,而不是0.00。
- 当价格字段为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
- When the price field has 7.8 then it will return 7.8 instead of 7.80
- When the price field has 0 then it will return 0.0 instead of 0.00
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论