User Tools

Site Tools


rounding_numbers

Table of Contents

If you want to send us your comments, please do so. Thanks
More on comments


Rounding numbers

Sources

Rechnen in bash ( German )

Library

file: libmath.sh

#! /bin/bash
 
# Round value to n places
# -----------------------
# Options:	value, places to round to
# Return value:	rounded value
# Use:		round floatnumber decimalplaces returnvalue
 
function round()
{
  # Change a colon to a dot since bc only works with dot as a decimal separator\\
  #  whatever the locale is set to
  value1=$(echo $1 | tr , . )
  value2=$(echo "scale=$2;(((10^$2)*$value1)+0.5)/(10^$2)" | bc)
  # Change the dot back to a colon. Comment the next line out if you do not need this
  value3=$(echo $value2 | tr . , )
  eval "$3='$value3'"			# The return string
};

Usage example

#! /bin/bash
 
. /pathtolib/libmath.sh
 
amount=4567,4593
round $amount 2 amountR
echo -e "Amount: $AmountR"

Result:
Amount: 4567,46


Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
rounding_numbers.txt · Last modified: 17-11-2015 14:17 by wim