Java的浮点数和整数的进制转换
| -6 | 5 | |
| 原码 | 10000110 | 00000101 |
| 反码 | 11111001 | 01111010 |
| 补码 | 11111010 | 00000101 |
补码运输的例子:
-6+5 -4+5
11111010 11111100
+ 00000101 + 00000101
= 11111111 = 00000001
int a=-6;
for(int i=0;i<32;i++){
int t=(a & (0x80000000)>>>i)>>>(31-i);
System.out.print(t);
}
| IEEE754 | 符号位 | 指数位 | 尾数位 | 指数偏移量 | 计算公式 | 附加位 |
| float(单精度浮点数) | 1[31] | 8[23-30] | 23[0-22] | 127 | s*m*2^(e-127) | e全0 尾数附加位为0 否则为1, 附加位对应2^0 |
| double(双进度浮点数) | 1[63] | 11[52-62] | 52[0-51] | 1023 | s*m*2^(e-1023) | e全0 尾数附加位为0 否则为1, 附加位对应2^0 |
-5的IEEE 754表示.
11000000101000000000000000000000
-1*2^(129-127)*(2^0+2^-2) 红色的部分对应附加位
一个IEEE754在线小工具: http://www.h-schmidt.net/FloatConverter/IEEE754.html
打印float的IEEE754表示的二进制串
public class FloatString {
public static void main(String[] args){
float f = 100.2f;
int intString = Float.floatToIntBits(f);
if(intString!=0x7f800000||intString!=0xff800000||intString!=0x7fc00000)
{
for(int i =0;i<32;i++)
{
int t =( intString & (0x80000000>>>i) ) >>> (31-i);
System.out.print(t);
}
}
}
}
Java 的进制间转换.
十进制转成十六进制:Integer.toHexString(int i)
十进制转成八进制:Integer.toOctalString(int i)
十进制转成二进制:Integer.toBinaryString(int i)
十进制转成别的进制的通用方法:Integer.toString(1, 8);
十六进制转成十进制:Integer.valueOf("FFFF",16).toString()
八进制转成十进制:Integer.valueOf("876",8).toString()
二进制转十进制:Integer.valueOf("0101",2).toString()
转成十进制的通用方法 Integer.parseInt(s, radix) /Integer.valueOf(s, radix)
Float 到double的转换错误例子:
问题1:double类型转float类型没有出错,float转double类型出错。
float f =
121.1f;
double d =
f;
System.out.println(d);
输出:
121.0999984741211
解决办法1:
float
f = 121.1f;
double d ;
BigDecimal bd = new
BigDecimal(String.valueOf(f));
d =
bd.doubleValue();
System.out.println(d);
输出:
121.1
问题2:
double
d =
(3.3-2.1)/0.1;
System.out.println(d);
输出:
11.999999999999996
解决办法2:
BigDecimal b = new BigDecimal("3.3").subtract(new BigDecimal("2.1")).divide(new BigDecimal("0.1"));
System.out.println(b.doubleValue());
小数运算时,或可能会产生小数的运算时,都转换成BigDecimal后再进行,得到结果后再转换成相应类型。
如果感觉麻烦,可以写一个基本运算的工具类,参考博客
说明:
java中整数默认是int,小数默认是double类型。(如要得到float类型,则加f如:12.1f)
int:4字节,long:8字节,float:4字节,double:8字节
float类型的变量只有7位的精度,而double类型的变量有15位的精度。
java在运算时会自动的提升变量的精度来进行运算。(double比float精度更高,所以可以自动的从float转化至double再进行运算。)
BigDecimal介绍:
float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用java.math.BigDecimal.
BigDecimal(double val) Translates a double
into a BigDecimal.这个方法会丢失精度.下面是该API中的说明.
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
BigDecimal(String val) Translates the String
repre sentation of a BigDecimal into a BigDecimal. 这个方法不会丢失精度.
做浮点数运算的工具类
import java.math.BigDecimal;
/**
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
* 确的浮点数运算,包括加减乘除和四舍五入。
*/
public class Arith{
//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//这个类不能实例化
private Arith(){
}
/**
* 提供精确的加法运算。
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
public static double add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
* 提供精确的减法运算。
* @param v1 被减数
* @param v2 减数
* @return 两个参数的差
*/
public static double sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.suBTract(b2).doubleValue();
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后10位,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @return 两个参数的商
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static double div(double v1,double v2,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
};
参考:http://blog.chinaunix.net/uid-26434689-id-3554399.html
Java的浮点数和整数的进制转换的更多相关文章
- 【JAVA随摘笔记一】进制转换
// 十进制转其它进制(二进制,八进制,十六进制) ; System.out.println(Integer.toBinaryString(k));// 转二进制 10001 System.out.p ...
- JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算
JAVA之旅(一)--基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算 Android老鸟重新学一遍JAVA是什么感觉?枯燥啊,乏味啊,而且归纳写博客,都 ...
- Java进制转换, 数据类型, 运算符
1:进制转换 转换规则: 先把数据的每一位上的系数乘以对应基数的次幂(低位从零开始),然后相加即可 十进制到其他进制 规则:除基取余,直到商为0,最后将余数反转 十进制到二进制: 除2取余,直到商为0 ...
- java se系列(二) 关键字、注释、常量、进制转换、变量、数据类型转换、运算符
1 关键字 1.1 关键字的概述 Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名.方法名.类名.包名. 1.2 常见的关键字 备注 ...
- 2 Java基础语法(keyword,标识符,凝视,常量,进制转换,变量,数据类型,数据类型转换)
1:keyword(掌握) (1)被Java语言赋予特定含义的单词 (2)特点: 所有小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这种高级记事本会对 ...
- JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
主函数类MainDemo.java package com.itcast.test20140109; import java.util.ArrayList; import java.util.Coll ...
- 《java版进制转换》
import java.util.Scanner; class 十进制转成十六进制_2 { public static void main(String[] args) { int num = 0; ...
- Java:进制转换
进制转换是常常需要的一种数据处理,在java中的一些类中封装了具有转换功能的方法,这个不做介绍.其实,进制之间的转化是通过先位异或&,再位移动>>>的方式实现的. 例如,对于 ...
- Java基础复习之一篇:关健字,标识符,注释,常量,进制转换,变量,数据类型,数据类型转换
1.关健字 1.1.被Java语言赋予特定意义的单词(如:class,interface,public ,static) 1.2.全部是小写 1.3.注意事项(goto和const作为关健字保留起来) ...
随机推荐
- CSS3时钟式进度条
CSS3时钟式进度条,加载完成时生成一个圆,数字慢慢变成100,适时的显示加载进度.友情提醒,如果预览时网页左下角提示错误,刷新一下就可以看到效果了:实际使用中不会出现这样的问题. <!DOCT ...
- Git Shell使用笔记
1,首次打开Git shell错误(以前打开过gethub客户端) 警告: git command could not be found. Please create an alias or add ...
- MyBatis 配置sql语句输出
版权声明:本文为博主原创文章,未经博主允许不得转载. 此处使用log4j,加入jar包,然后在src路径下加入:log4j.properties文件 填入以下配置就可以打印了 log4j.rootLo ...
- HTTP 笔记与总结(1 )Telnet 分别发送 HTTP GET 和 HTTP POST 请求
简化的说: WebService = HTTP 协议 + XML Rest = HTTP 协议 + Json 各种 API = HTTP 协议 + XML / Json 来实现 HTTP 请求信息格式 ...
- 运维工程师必会的109个Linux命令
运维工程师必会的109个Linux命令 版本1.0 崔存新 更新于2009-12-26 目录 1 文件管理 6 1.1 basename 6 1.2 cat 6 1.3 cd 7 1.4 chgrp ...
- RT-Thread的线程间同步
禁止系统调度上一节< 多线程导致的临界区问题>中由于 test1 线程被 test2 线程打断,才导致了我们没有得到预期的结果,我们一般可通过关闭中断和调度器上锁这两种简单的途径来禁止系统 ...
- The "get" method should be used when the form is idempotent---正交的两个概念---
https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1 17.13.1 Form submission method The me ...
- ubuntu下各个软件完全卸载
1.卸载mysql sudo rm /var/lib/mysql/ -R删除mysql的数据文件2sudo rm /etc/mysql/ -R删除mqsql的配置文件3sudo apt-get aut ...
- arcgis server site 快速恢复与重建
背景 在实际的工作中,会遇到如下的情形: arcgis server服务能够启动,但是由于未知的操作原因导致site里面的某个功能出现故障. 要想在短时间内,使得server能够恢复正常功能.且: 不 ...
- 【转】javascript 中that的含义示例介绍
var that = this;,这代表什么意思呢?this代表的是当前对象,var that=this就是将当前的this对象复制一份到that变量中,下面为大家介绍这样做有什么意义 你可能会发现别 ...