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



你可能感兴趣的:(Java,MapReduce,HBase)