java中指定位数,不足的在前面补“0”,用于拼接RowKey;以及java中去掉小数点后面多余的“0”;

4 篇文章 0 订阅
2 篇文章 0 订阅

问题描述:

        查询Hbase时,使用RowKey查询效率会很高,但有时候在已知RowKey由哪几部分字段组成时,会有拼接字符串为RowKey到hbase查询的场景。但是RowKey设计为每段位固定的长高度,不足的需要补“0”。所以,在java程序中,需要调用函数设置固定位数,并用“0”补足。

比如将一位的int“2”,处理成四位的“0002”字符串:

	public static void main(String[] args) {
		 int str=2;
         DecimalFormat df=new DecimalFormat("0000");
         System.out.println(df.format(str));
	}

0002

参考地址:https://blog.csdn.net/weimeng1991/article/details/42964715

问题描述:

        在使用Integer.parseInt(),将数字字符串解析成Int类型时,如果数字字符串的小数点后面有“0”则会报错,如下:

	public static void main(String[] args) throws ParseException {
		String s="1.0";
		Integer i=Integer.parseInt(s);
	}
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.0"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at numbertest.StringToInt.main(StringToInt.java:9)

解决办法,修改如下:

	public static void main(String[] args) throws ParseException {
		String s="1.0";
		DecimalFormat df=new DecimalFormat("0");
		int i=df.parse(s).intValue();
		System.out.println(i);
	}
1



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值