why printf doesn't work it shouldn't be 1,00

Viewed 66

the output should be 1.66 but it is 1,00 why? I install brew findutils but it doesn't help

#!/bin/bash
#bc - l
x=$1
y=$2
t=$(echo "$x/$y " | bc -l | awk '{printf("%.2f \n",$1)}')
echo $t

sh script1.bash 5 3
1,00
1 Answers

if u insist on 1.66 instead of 1.67, try

mawk '($++NF=+(__=$(_+=_^=_<_)) ? int(($--_)*(_*=_+=++_*_*_)/__)/_ \
                                : "_ERR_DIV_Zero_")^!_' CONVFMT='%.2f' <<< '5 3'
5 3 1.66

if u don't mind the 1.67 instead :

gawk '($++NF=+(__=$(_+=_^=_<_)) ? ($--_)/__ \
                                : "_ERR_DIV_Zero_")^!_' CONVFMT='%.2f' <<< '5 3'
5 3 1.67

if u just want a brute-force approach to get a sizable amount of exact digits without arbitrary precision add-ons :

{m,g}awk '

 function _____(___,__,_,____) {
 1  return !+__ \
    ? "_ERR_DIV_ZERO_" \
    : int((_+=_^=_<_)^(_+_+_)*((_+=_)+_+_))<=+PREC \
    ? sprintf("%.f.%0*.f", int(___/__),
              ____+=____=(_+=++_)*_, int(((___%__)\
                * (_+=(_+=_^=_<_)*_*_)^____)/__))  \
    : sprintf(("%.f.")(_="%0"(____=______(__))".f")\
                    (_=(_)(_)(_=(_)(_)_)) (_=(_)_)_,
      int((___=int(___))/(__=int(__))), 
      int((_=(___%__)*(___=int((_+=\
         (_+=_^=_<_)*_*_)^____) ) )/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
      int((_=_%__*___)/__), int((_=_%__*___)/__), int((_=_%__*___)/__),
                            int((_=_%__*___)/__), int((_=_%__*___)/__),
                            int((_=_%__*___)/__), int((_=_%__*___)/__))
 }
 function ______(____,_,__,___) {
1    ___=(__+=(\
      __=_+=_^=_<_)*_*_)^(++_+_*_+_)*_*_+(_=_<_)

8    while((____*=__)<___) { ++_ }
     return +_
 }
 ($++NF=_____($(_+=_^=_<_), $++_))^!_' CONVFMT='%.20g' FS='[ ]' \
      OFS='\f\r\t' ORS='\n\n' <<< ' 8777777777777777 23456789'
 8777777777777777
 23456789
 374210544.238504980370501691429291536876594660931639023567974286676
 6632039875534541407180667396547754255708230141815233107992743593336
 6668387561485930576431411818557092362471265781518519009571173616303
 57846506612648474

output from gnu-bc for the same division ::

echo 'scale = 208; 8777777777777777 / 23456789' | bc -l
 gnubc:

 374210544.238504980370501691429291536876594660931639023567974286676
 6632039875534541407180667396547754255708230141815233107992743593336
 6668387561485930576431411818557092362471265781518519009571173616303
 57846506612648474
Related