当前位置:网站首页>Beautify multiple digits
Beautify multiple digits
2022-07-22 16:27:00 【My talented girlfriend】
#!/bin/bash
# nicenumber -- Display the given number in comma separated form
# Two options are acceptable :DD(decimal point delimiter, Decimal separator ) and TD(thousands deliniter, Thousand separator )
# Beautify digital units , If a second parameter is specified , Then the output is echoed in stdout
nicenumber(){
# Note that we assume ‘.’ Is the decimal separator of the script input value
# Unless the user specifies options -d Other delimiters are specified , Otherwise, the decimal separator in the output value is also .
# If there is no corresponding field in the character, all columns return the corresponding original value
integer=$(echo $1 | cut -d. -f1)
decimal=$(echo $1 | cut -d. -f2)
if [ "$integer" != "$1" ]; then
# ${DD:= '.'} If the variable is undefined , Set to . If there is value, keep the original value
result="${DD:= '.'}$decimal"
fi
thousands=$integer
while [ $thousands -gt 999 ]; do
# $(()) yes shell Used for calculation
remainder=$(($thousands % 1000))
while [ ${
#remainder} -lt 3 ]; do
remainder="0$remainder"
done
result="${TD:= ','}${remainder}${result}"
thousands=$(($thousands / 1000))
done
nicenum="${thousands}${result}"
echo " The second parameter "$2
if [ ! -z $2 ]; then
echo $nicenum
fi
}
DD="."
TD=","
# getopts Is used to parse parameters ,d:t: Namely -d -t Parsing assigned variables opt
while getopts "d:t:" opt; do
case $opt in
d ) DD="$OPTARG" ;;
t ) DD="$OPTARG" ;;
esac
done
# Remove the corresponding parameter
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) [ -d c ] [ -t c] number_value"
echo " -d specifies the decimal point delimiter (default '.')"
echo " -t specifies the thousands delimiter (default ',')"
exit 0
fi
# Force function echo
nicenumber $1 1
exit 0
边栏推荐
- Figure calculation - figure introduction
- SoC 自定义 IP 核--呼吸灯
- window开机启动增加/关闭
- 第四讲 ssh
- Typora free download compressed package (latest available)
- The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
- Android interview: 2022 please keep this experience of Netease Android development and Tiktok e-commerce Android engineers
- 万亿市值公链竞争白热化,新公链还有机会吗?
- C # upload files to shared folders
- RquestMapping的注解功能、注解使用范围、注解的属性详情
猜你喜欢
Scala variables and data types (2)
JVM内存模型:虚拟机栈
Rosen's QT journey 98 QML tab control tabview
SoC之Hello World
Soc之按键控制LED
Lecture 7 pipeline, environment variables and common commands
C#服务器NFS共享文件夹搭建与上传图片文件
【解决】npm ERR! code E401
科普 | 如何创建一个DAO?
[elaborate] ES6's array expansion method, object expansion method, string expansion method Object level depth
随机推荐
[solution] solve typeerror: FC () got an unexpected keyword argument 'is when paddlepaddle runs reinforcement learning code_ Test 'error
Scala variables and data types (2)
Hybrid混合开发与JSBridge
进程和线程面试问题
The big problem of HA automatic failover (active) namenode
EasyCVR平台设备分组新增以及编辑操作异常的问题修复
【leetcode】
STM32基于HAL库的非DMA的轮询ADC单通道与多通道的采样
mysql通过开启全局日志进行定位排查慢sql
Android面试:2022请收好这份网易Android开发和抖音电商Android工程师的面经
C# 上传图片至共享文件夹
SoC 自定义 IP 核--呼吸灯
Mock simulates data and initiates get and post requests (nanny level tutorials are sure to succeed)
2022那些测试员面试中的“潜规则”,千万不要踩坑....
[solution] NPM err! code E401
【解决】npm ERR! code E401
Alternet scripting, user interface design function
盒马两大供应链中心启用 多业态商品创新研发“有后台”
数据平台数据管理实践
Rosen's QT journey 98 QML tab control tabview