jq以科学计数形式重新格式化小数-可以避免这种情况吗? [英] jq reformatting decimals in scientific notation -- can this be avoided?
问题描述
我发现JavaScipt和通过bash(和其他编程语言)通过jq
创建的json数据之间存在差异.使用JavaScript,即使使用float()
,我也可以创建点后最多六位的十进制数字.但是与jq
有所不同,因为添加十进制值仅需要在小数点后四位数.
I found difference between json-data created by JavaScipt and via jq
with bash (and other programming languages). With JavaScript I can create decimal numbers with up to six digits after the point, even when I use float()
. But with jq
its different, because adding a decimal value takes four digits after the decimal point only.
我的问题是我需要十进制数字存储在SQL中,该点后最多六位数字.
My problem is that I need decimal numbers to store in SQL, with up to six digits after the point.
示例:
$ JSON='{"decimal":0.00001}'
$ echo "$JSON" | jq .
{
"decimal": 1e-05
}
我的目标是使用此行来验证小数点...
My goal is to validate the decimal with this line ...
if [[ "$TMP_DECIMAL" =~ ^[0-9]+([.][0-9]+)?$ ]] ; then
有任何建议/建议吗?
推荐答案
You can't change jq
's behavior -- at present date, relevant feature requests are still open -- but you can reformat your numbers after they've been retrieved. For example:
json='{"decimal":0.00001}'
decimal=$(jq '.decimal' <<<"$json")
decimal_f=$(awk -v decimal="$decimal" 'BEGIN { printf("%f\n", decimal) }' </dev/null)
echo "JQ emitted $decimal; reformatted as $decimal_f"
这篇关于jq以科学计数形式重新格式化小数-可以避免这种情况吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!