Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq reformatting decimals in scientific notation -- can this be avoided?

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.

My problem is that I need decimal numbers to store in SQL, with up to six digits after the point.

Example:

$ 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

Any tip / suggsestion ?

like image 607
Mario Avatar asked Mar 22 '17 15:03

Mario


People also ask

Does scientific notation need a decimal?

Scientific notation is a way to make these numbers easier to work with. In scientific notation, you move the decimal place until you have a number between 1 and 10. Then you add a power of ten that tells how many places you moved the decimal. In scientific notation, 2,890,000,000 becomes 2.89 x 109.

How to restrict decimal value in jQuery?

Restrict (Prevent) to 2 decimal places in KeyPress after decimal point in JavaScript and jQuery. $( this ). val(text + "." )

Should scientific notation be rounded?

For the most accurate result, you should always round after you preform the arithmetic if possible. When asked to do arithmetic and present you answer rounded to a fixed number of decimal places, only round after performing the arithmetic. Round the answer to 2 decimal places.

Do you move the decimal to the left or right scientific notation?

To write a number in scientific notation:Move the decimal to a position immediately to the right of the first nonzero digit. value of the exponent. - If you moved the decimal point to the left, make the exponent positive. - If you moved the decimal point to the right, make the exponent negative.


2 Answers

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"
like image 68
Charles Duffy Avatar answered Sep 19 '22 19:09

Charles Duffy


Also, you can reformat your JSON using perl module JSON::PP.

perl -0777 -MJSON::PP -E '$s=<>; $j=JSON::PP->new->ascii->pretty->allow_nonref->allow_bignum;$p=$j->decode($s);say $j->encode($p)'

or nicer:

perl -0777 -MJSON::PP -E '
    $j=JSON::PP->new->ascii->pretty->allow_nonref->allow_bignum;
    $p=$j->decode(<>);
    say $j->encode($p)'

The crucial is the allow_bignum.

Example:

echo '{"decimal":0.00000001}' | perl ....

prints

{
   "decimal" : 0.00000001
}

but without the allow_bignum prints

{
   "decimal" : 1e-08
}

Ps: ... and also, is possible to validate the whole json using perl... :)

like image 29
jm666 Avatar answered Sep 20 '22 19:09

jm666