LINUX + KSH +圆形或倒圆起来 - 浮点数 [英] linux + ksh + Round down or Round up - float numbers

查看:149
本文介绍了LINUX + KSH +圆形或倒圆起来 - 浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ksh脚本我需要计算只有整数

in my ksh script I need to calculate only integer numbers

有时我浮点数,如3.49或4.8 ...等

Sometimes I get float numbers such as 3.49 or 4.8...etc

所以我需要按照以下规则的浮点数转换为整数的数字(例子)

so I need to translate the float numbers to integer’s numbers according to the following rules (examples)

3.49     will be 3
2.9      will be 3
4.1      will be 4
23.51    will be 24
982.4999 will be 982

10.5     will be 11  ( this example if float is .5 then it will roundup )

请指点如何在KSH或awk完成这或Perl

Please advice how to do this in ksh or awk or perl

或者

这可以在我的ksh脚本运行任何其他语言

any other language that can be run in my ksh script

推荐答案

短暂的谷歌会后,我发现,的printf 似乎是能够做的工作,在至少在bash(无法找到一个在线间preTER,做KSH)。

After a brief google session, I found that printf seems to be able to do the job, at least in bash (couldn't find an online interpreter that does ksh).

printf "%0.f\n" 4.51
5
printf "%0.f\n" 4.49
4

code: http://ideone.com/nEFYF

请注意:perl的可能是矫枉过正,像马吕斯说,但这里有一个Perl的方式:

Note: perl might be overkill, like Marius says, but here's a perl way:

该Perl模块​​数学::圆形似乎处理工作。

The perl module Math::Round seems to handle the job.

单行:

perl -MMath::Round -we 'print round $ARGV[0]' 12.49

脚本:

use v5.10;
use Math::Round;
my @list = (3.49, 2.9, 4.1, 23.51, 982.4999);

say round $_ for @list;

脚本输出:

3
3
4
24
982

这篇关于LINUX + KSH +圆形或倒圆起来 - 浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆