科学记数法-指数E数字的处理

本文详细介绍了Java中浮点数转换为整数的方法,并通过实例展示了如何使用BigDecimal进行大数处理,确保数值计算的精确性和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

<fmt:formatNumber value="${Money }" pattern="#.##" minFractionDigits="2"></fmt:formatNumber>
 

 

public static void main(String[] args) {
		String ss = "9.86";
		Float ff =  Float.valueOf(ss);//float精度7
		ff =ff*100;
		System.out.println(ff);
		long result = ff.longValue();
		System.out.println(result);
		
		Double dd= Double.valueOf(ss);//double精度16
		BigDecimal bigD = new BigDecimal(dd);
		bigD = bigD.multiply(new BigDecimal(100)).divide(new BigDecimal(1), 1, BigDecimal.ROUND_HALF_UP);
		Long result2 = bigD.longValue();
		System.out.println(result2);
		
        MathContext mc = new MathContext(10); // 5 precision -- 5个有效数字
        BigDecimal bg = new BigDecimal("1.23456789E+10",mc);
        String s = bg.toPlainString();
        String str = "Plain string value of " + bg + " is " + s;
        System.out.println( str );
        
        float f = 12345678900000f;
        double d = 12345678900000d;
        System.out.println(new BigDecimal(f,mc).toPlainString());//12345679020000
        System.out.println(new BigDecimal(d,mc).toPlainString());//12345678900000
	}

 

参考链接:http://blog.youkuaiyun.com/ugg/article/details/8213666

在Java中,如果`Integer`对象表示的是一个科学记数法的数值,你可以通过以下步骤将其转换为十进制整数: 1. 首先,将`Integer`对象转换为`String`类型,因为科学记数法在字符串形式下更容易处理。可以使用`toString()`方法获取字符串。 ```java String scientificNum = Integer.toString(yourInteger); ``` 2. 然后,解析这个字符串,去掉前导的"0."(如果是小数),并移除'e'或'E'以及其后的指数部分。可以使用正则表达式或者手动切分字符串来完成这一步。 ```java // 使用正则表达式 Pattern pattern = Pattern.compile("\\d+\\.?e-?\\d+"); Matcher matcher = pattern.matcher(scientificNum); if (matcher.find()) { String decimalPart = matcher.group(); // 如果有负指数,则需要处理 int exponent = scientificNum.contains("E") ? scientificNum.substring(scientificNum.indexOf('E') + 1).toIntValue() : scientificNum.endsWith("e-1") ? -1 : 0; return Double.parseDouble(decimalPart) * Math.pow(10, exponent).toIntValue(); } ``` 或者手动处理: ```java int index = scientificNum.indexOf('e'); if (index != -1) { String integerPart = scientificNum.substring(0, index); int exponent = scientificNum.substring(index + 1).toIntValue(); return Integer.parseInt(integerPart) * Math.pow(10, exponent); } else if (scientificNum.startsWith("0.")) { // 移除前导零,并转为整数 scientificNum = scientificNum.substring(2); return Integer.parseInt(scientificNum); } else { return Integer.parseInt(scientificNum); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值