How to Display Number to Two Decimal Places in Bash Function

How to display number to two decimal places in bash function

Bash has a printf function built in:

printf "%0.2f\n" $T

Bash, need to count variables, round to 2 and store to variable

Yes, it is working! I did it like this.

Arithmetic operations:

pricevat=$(echo "$vat * ${array[5]}" + ${array[5]} | bc -l)

Round to 3 places:

pricevat=$(printf "%0.3f\n" $pricevat)

If there is another way to do it better or together on one line, let me know please.

Thanks.

How to round a floating point number upto 3 digits after decimal point in bash

What about

a=`echo "5+50*3/20 + (19*2)/7" | bc -l`
a_rounded=`printf "%.3f" $a`
echo "a = $a"
echo "a_rounded = $a_rounded"

which outputs

a         = 17.92857142857142857142
a_rounded = 17.929

?

Sorting numbers with multiple decimals in bash

You need the -t. flag to specify '.' as your separator, and the multiple key position specifiers handles the progressively longer/deeper numbers. I still don't quite understand exactly how it works, but it works ...

 sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n numbers

or

 cat numbers | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n

JavaScript displaying a float to 2 decimal places

float_num.toFixed(2);

Note:toFixed() will round or pad with zeros if necessary to meet the specified length.

How to format output as a decimal with two decimal places

One possibility is to set OFMT, but note that this won't produce a fractional part for values that happen to be integers:

$ awk -v OFMT=%.2f \
'{sum[$1] += $2; counts[$1]++;} END {for (i in sum) print i, sum[i]/counts[i];}' \
<<END
a 5
a 4
b 8
b 10
c 4
c 4
c 5
END
a 4.50
b 9
c 4.33

A more reliable method is to use printf instead of print:

$ awk \
-e '{sum[$1] += $2; counts[$1]++;}' \
-e 'END {for (i in sum) printf "%s %.2f\n", i, sum[i]/counts[i];}' \
<<END
a 5
a 4
b 8
b 10
c 4
c 4
c 5
END
a 4.50
b 9.00
c 4.33

Get Average of Found Numbers in Each File to Two Decimal Places

In general, your script does not extract all numbers from each file, but only the first digit of the first number. Consider the following file:

<Overall>123 ...
<Overall>4 <Overall>56 ...
<Overall>7.89 ...
<Overall> 0 ...

The command grep '<Overall>' | head -c 10 | tail -c 1 will only extract 1.

To extract all numbers preceded by <Overall> you can use grep -Eo '<Overall> *[0-9.]*' | grep -o '[0-9.]*' or (depending on your version) grep -Po '<Overall>\s*\K[0-9.]*'.

To compute the average of these numbers you can use your awk command or specialized tools like ... | average (from the package num-utils) or ... | datamash mean 1.

To print numbers with two decimal places (that is 1.00 instead of 1 and 2.35 instead of 2.34567) you can use printf.

#! /bin/bash
path=/home/Downloads/scores/
for i in "$path"/*; do
avg=$(grep -Eo '<Overall> *[0-9.]*' "$file" | grep -o '[0-9.]*' |
awk '{total += $1} END {print total/NR}')
printf '%s %.2f\n' "$(basename "$i" .dat)" "$avg"
done |
sort -g -k 2

Sorting works only if file names are free of whitespace (like space, tab, newline).

Note that you can swap out the two lines after avg=$( with any method mentioned above.

How do I format a number to 2 decimal places, but only if there are already decimals?

Working example: http://jsfiddle.net/peeter/JxPZH/

$(document).ready(function() {    $('#itemQuantitySelect_3').change(function() {                var itemPrice = 1.50;        var itemQuantity = $(this).val();        var quantityPrice = (itemPrice * itemQuantity);        if(Math.round(quantityPrice) !== quantityPrice) {            quantityPrice = quantityPrice.toFixed(2);        }                $(this).next("span").html("$" + quantityPrice);
});});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form action="/" method="post">    <select id='itemQuantitySelect_3' name="itemQuantity_3">        <option value='1'>1 Item</option>        <option value='2'>2 Items</option>        <option value='3'>3 Items</option>    </select>    <span>$1.50</span></form>


Related Topics



Leave a reply



Submit