division par 1000 en bash

Oct 06, 2021 11 Replies

Hello



ennuient mais??



#/bin/bash



TOT=21765



let NUM=$TOT/1000



(( NUU=$TOT/1000))



echo "$TOT $NUM $NUU"



le resulta est 21


Merci d'avance


Bash use ONLY INTEGER numbers!

Read man bash!

TOT=21765 NUM=`echo $TOT / 1000 |bc -l` echo "$TOT $NUM"

But don't be fooled, $NUM is a string, not a floating point number

Yes. You can install 'bc' to do more math in scripts, for example:

R=$(echo 'scale=3;21765/1000' | bc -l) echo $R

One drawback: it has no rounding, only truncation, so you'd have to improvise with +0.5 or use printf.

In Javascript and many other languages, there is no difference.

Truth welcomes investigation because truth knows investigation will lead to converts. It is deception that uses all the other techniques.

Irrelevant. The topic of this conversation is Bash, not javascript nor "many other languages".

Your digression seems meant only to confuse the issue under discussion.

Lew Pitcher "In Skills, We Trust"

Also it?s wrong. Strings and numbers are distinct types in JS.

formatting link

https://www.greenend.org.uk/rjk/

In article (Dans l'article) , zeneca

Voltaire.

Va donc voir sur fr.comp.sys.raspberry-pi

Jean-Pierre Kuypers

They are typed, but it's weak rather than strong, and has automatic promotion

str = "1.570795"; num = 1.570795; res = 2 * str; alert([typeof(str), typeof(num), res, typeof(res)]);

A. Dumas:

Or you can keep truncation, but set up one extra digit of precision...

() ascii ribbon campaign - against html e-mail /\ http://preview.tinyurl.com/qcy6mjc [archived]

How does that help?

echo 'scale=4;1/3*3' | bc -l

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required