「小数除法java」小数除法计算题
本篇文章给大家谈谈小数除法java,以及小数除法计算题对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java中怎么把除法精确到小数点后100位
JAVA中如何对double或者float的浮点数进行精度计算,
在JAVA中提供了多种参数来实现精度的不同控制方式。
具体例子如下:
package com.soft4j.utility;
import java.math.BigDecimal;
/**
* 与小数位精度(四舍五入等)相关的一些常用工具方法.
*
* float/double的精度取值方式分为以下几种: br
* java.math.BigDecimal.ROUND_UP br
* java.math.BigDecimal.ROUND_DOWN br
* java.math.BigDecimal.ROUND_CEILING br
* java.math.BigDecimal.ROUND_FLOOR br
* java.math.BigDecimal.ROUND_HALF_UPbr
* java.math.BigDecimal.ROUND_HALF_DOWN br
* java.math.BigDecimal.ROUND_HALF_EVEN br
*
* @author stephen
* @version 1.0.0
*/
public final class RoundTool {
/**
* 对double数据进行取精度.
* p
* For example: br
* double value = 100.345678; br
* double ret = round(value,4,BigDecimal.ROUND_HALF_UP); br
* ret为100.3457 br
*
* @param value
* double数据.
* @param scale
* 精度位数(保留的小数位数).
* @param roundingMode
* 精度取值方式.
* @return 精度计算后的数据.
*/
public static double round(double value, int scale, int roundingMode) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, roundingMode);
double d = bd.doubleValue();
bd = null;
return d;
}
/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
//下面都以保留2位小数为例
//ROUND_UP
//只要第2位后面存在大于0的小数,则第2位就+1
System.out.println(round(12.3401,2,BigDecimal.ROUND_UP));//12.35
System.out.println(round(-12.3401,2,BigDecimal.ROUND_UP));//-12.35
//ROUND_DOWN
//与ROUND_UP相反
//直接舍弃第2位后面的所有小数
System.out.println(round(12.349,2,BigDecimal.ROUND_DOWN));//12.34
System.out.println(round(-12.349,2,BigDecimal.ROUND_DOWN));//-12.34
//ROUND_CEILING
//如果数字0 则和ROUND_UP作用一样
//如果数字0 则和ROUND_DOWN作用一样
System.out.println(round(12.3401,2,BigDecimal.ROUND_CEILING));//12.35
System.out.println(round(-12.349,2,BigDecimal.ROUND_CEILING));//-12.34
//ROUND_FLOOR
//如果数字0 则和ROUND_DOWN作用一样
//如果数字0 则和ROUND_UP作用一样
System.out.println(round(12.349,2,BigDecimal.ROUND_FLOOR));//12.34
System.out.println(round(-12.3401,2,BigDecimal.ROUND_FLOOR));//-12.35
//ROUND_HALF_UP [这种方法最常用]
//如果第3位数字=5,则第2位数字+1
//备注:只看第3位数字的值,不会考虑第3位之后的小数的
System.out.println(round(12.345,2,BigDecimal.ROUND_HALF_UP));//12.35
System.out.println(round(12.3449,2,BigDecimal.ROUND_HALF_UP));//12.34
System.out.println(round(-12.345,2,BigDecimal.ROUND_HALF_UP));//-12.35
System.out.println(round(-12.3449,2,BigDecimal.ROUND_HALF_UP));//-12.34
//ROUND_HALF_DOWN
//如果第3位数字=5,则做ROUND_UP
//如果第3位数字5,则做ROUND_DOWN
System.out.println(round(12.345,2,BigDecimal.ROUND_HALF_DOWN));//12.35
System.out.println(round(12.3449,2,BigDecimal.ROUND_HALF_DOWN));//12.34
System.out.println(round(-12.345,2,BigDecimal.ROUND_HALF_DOWN));//-12.35
System.out.println(round(-12.3449,2,BigDecimal.ROUND_HALF_DOWN));//-12.34
//ROUND_HALF_EVEN
//如果第3位是偶数,则做ROUND_HALF_DOWN
//如果第3位是奇数,则做ROUND_HALF_UP
System.out.println(round(12.346,2,BigDecimal.ROUND_HALF_EVEN));//12.35
System.out.println(round(12.345,2,BigDecimal.ROUND_HALF_EVEN));//12.35
}
}
相关文章:
《Double精度的常用设置》
import java.text.DecimalFormat;
import java.math.BigDecimal;
public class Test_Double{
public static void main(String[] args){
//-----方法1--------四舍五入 round对负数是五舍六入
double d_1 = 123.9;
System.out.println("d_1 = "+Math.round(d_1));
//-------方法2------------------
DecimalFormat decfmt = new DecimalFormat("##0.00");
System.out.println(decfmt.format(1.33482222));
//--------方法3--------------
double x = 1.33345;
java.text.NumberFormat formate = java.text.NumberFormat.getNumberInstance();
formate.setMaximumFractionDigits(3);//设定小数最大为数,那么显示的最后会四舍五入的
String m = formate.format(x);
System.out.println(m);
//--------方法4--------------
BigDecimal bd = new BigDecimal(1.234543);
bd = bd.setScale(3,BigDecimal.ROUND_HALF_EVEN);
double d = bd.doubleValue();
System.out.println(d);
//--------取消科学计数法-------------
Double dValue = Double.valueOf("276363652844.8477474");
System.out.println(dValue);
BigDecimal original = new BigDecimal(dValue.doubleValue());
BigDecimal result = original.setScale(2, BigDecimal.ROUND_HALF_DOWN);
String test = result.toString();
System.out.println(test);
}
}
java 除法怎样取小数部分
(((double)lili1)/lili)
-
(lili1/lili)就可以了,如果想显示出小数,必须要将数值转换为float或double类型才可以,推荐double类型
java一个整数除以一个小数为什么的到小数
Java中如果除运算符“/”,在不加任何限制的情况下,两个整数相除,得到的是整数,小数点后的被舍弃。但是有些场景下我们需要拿到除得的小数,还要指定位数的小数。这时候有以下处理方法:
1.使用DecimalFormat来限定得到的小数位数
int pcm = 98;
int fcm = 11;
DecimalFormat df = new DecimalFormat("0.00");
double tmpVal = Double.parseDouble(df.format((double) pcm/(pcm+fcm)));
//get value 0.89
注意,它默认返回的是String,如果需要double/float要做一下转换。
2.直接使用Decimal运算
@Test
public void testDecimalOper(){
int pcm = 94;
int fcm = 11;
BigDecimal pcmbd = new BigDecimal(pcm);
BigDecimal fcmbd = new BigDecimal(fcm);
BigDecimal rate = new BigDecimal(0.00);
rate = pcmbd.divide(pcmbd.add(fcmbd), 2, RoundingMode.HALF_UP);
System.out.println(rate);//0.90
}
float/double在工程运算中使用的比较多,在商业计算中使用Decimal类型的比较多。(注:
在《Effective Java》这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal,另外,我们如果需要精确计算,要用String来够造BigDecimal。在《Effective Java》一书中的例子是用String来够造BigDecimal的。(注意:divide方法中推荐使用枚举RoundingMode.HALF_UP)
)
两种方式都可以。推荐使用第二种方式来处理精度和round mode的设置。
附BigDecimal rouding mode:
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal } 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
public final static int ROUND_UNNECESSARY = 7;
JAVA 中除法后小数精度问题
你说的这个问题涉及到数字精度问题,在程序中,数字运算总是低精度的向高精度的自动转化。比如(int)/(float)就会得到你想要的小数数位,这是由数字在计算机中的运算方式决定的。如果你用3/5.0或者3.0/5,你就可以得到你想要的小数。
在计算机中,整型(int)和浮点(float,double)的表示形式各不相同,占用内存空间也不同,在运算时,为了得到正确的结果,就要进行对位,如果数据类型不一致,就要进行转化。在硬件层面上就是把占用内存少的整型向浮点转换,也就是为这个整型添加额外的字节参与运算,尽管这些字节中都是0,这是数字精度的问题。
现在是两个整形数,运算时,计算机认为不需要进行数据类型的转化,所以就得到你见到的结果。
小数除法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于小数除法计算题、小数除法java的信息别忘了在本站进行查找喔。