Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1282204
  • 博文数量: 213
  • 博客积分: 7590
  • 博客等级: 少将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-31 17:31
个人简介

热爱开源,热爱linux

文章分类

全部博文(213)

文章存档

2018年(4)

2017年(1)

2015年(1)

2014年(5)

2013年(2)

2012年(2)

2011年(21)

2010年(82)

2009年(72)

2008年(23)

分类: LINUX

2010-09-09 16:50:54

这几天有一个日志监控程序需要计算错误日志比例,要对小数进行比较,找了找网上资料并进行了一下总结
1.判断小数点后最多有几位数(N),然后对将要比较的两个数值进行 乘与10的N次方也就是将小数点去掉来进行比较(小数点后位数多的直接去掉小数点,少的用0补齐)
下面写了一个脚本进行小数的比较如下(我感觉到有点繁琐)


#!/bin/bash
# Script:cmp_decemial.sh

# Author:CaoJiangfeng

# Date:09/09/2010

# Purpose: This script is used to cmp two decemial number's bigness

#


# The numbers which are going to be comapred

a=$1
b=$2

#Obtain the number of decimal places after the decimal point

c=`echo "$a"|awk 'BEGIN{FS="."} {print length($2)}'`
d=`echo "$b"|awk 'BEGIN{FS="."} {print length($2)}'`

#Obtain the maximum number of decimal places after the decimal point

if [ $c -ge $d ];
then
    tmp=$c
else
    tmp=$d
fi

#Multiple variable expansion

temp=1

for ((i = 0; i < $tmp;i ++));
do
    temp=${temp}0
done
#After the expansion multiple of the value of temp

a1=`echo "$a*$temp" |bc`
b1=`echo "$b*$temp" |bc`

#Get integer part

c=`echo "$a1"|awk 'BEGIN{FS="."} {print $1}'`
d=`echo "$b1"|awk 'BEGIN{FS="."} {print $1}'`

#Compare the size of two numbers

if [ $c -gt $d ];
then
    echo "$a > $b"
elif [ $c -eq $d ];
then
    echo "$a = $b"
else    
    echo "$a < $b"
fi


2. 直接用awk
awk -v num1=6.6 -v num2=5.5 'BEGIN{print(num1>num2)?"0":"1"}'
如果num1>num2打印输出0,否则输出1
3.expr
比较妙的方法 expr $a \> $b
返回1表示$a>$b, 0表示$a<$b
# a=6.6 b=5.5;expr $a \> $b
1

阅读(14250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~