首页 > 编程知识 正文

bigdecimal加法,bigdecimal四舍五入

时间:2023-05-05 07:29:13 阅读:128016 作者:3037

二进制乘法

BigDecimal类的多tiple (方法) method syntax :

语法:

publicbigdecimalmultiply (bigdecimal m _ val; publicbigdecimalmultiply (bigdecimal m _ val,MathContext ma_co ); multiply (methodisavailableinjava.math包)。

multiple (方法可在java.math包中使用。

多点(bigdecimal m _ val ) )。 methodisusedgetabigdecimalthatholdsthevaluemultipliedthisbigdecimalbythegivenbigdecimalanditsscaleisculatedbyusing ()

使用乘法(BigDecimal m_val )方法获取BigDecimal。 此BigDecimal将BigDecimal乘以给定的BigDecimal,然后使用([thisBigDecimal.scale () ] )

多点(bigdecimal m _ val,math上下文ma _ co ) methodisusedtogetabigdecimalthatholdsthevaluemultipliedthisbigdecimalbytttthy

乘法(BigDecimal m_val,MathContext ma_co )方法获取BigDecimal,该BigDecimal包含基于指定MathContext设置将此BigDecimal乘以指定BigDecimal的值

thesemethodsmaythrowanexceptionatthetimeofmultipliedanobject。

这些方法在对象相乘时可能会引起异常。

arithmetic exception : thisexceptionmaythrowwhentheresultisnotaccurateandsettheroundingmode ' unnecessary '。

ArithmeticException :如果结果不正确,并且舍入模式设置为" UNNECESSARY ",则可能会发生此异常。

these are non-staticmethodsanditisaccessiblewithclassobjectsandifwetrytoaccessthesemethodsnamethenwewillgetaneror

这些非静态方法可以通过类对象访问。 如果尝试使用类名进行访问,将显示错误消息。

参数(s ) :

参数:

在第一个情况下,多重(bigdecimal m _ val ),

在第一种情况下,乘以(BigDecimal m_val ),

bigdecimal m _ val-representstheobjectisusedtomultiplythisbigdecimalobject.bigdecimal m _ val -要与此bigdecimal对象相乘In the first case,multiply(bigdecimalm_val,MathContext ma_co ),

在第一种情况下,乘法(BigDecimal m_val,MathContext ma_co ),

bigdecimal m _ valsimilarasdefinedinthefirstcase.bigdecimal m _ val与第一种情况中定义的类似。 mathcontextma _ corepresentsthecontextsettingtouseinrounding.mathcontextma _ co显示要四舍五入的上下文设置。 Return value:

返回值:

在In both the cases,thereturntypeofthemethodisbigdecimal,

在这两种情况下,方法的返回类型都是BigDecimal。

在第一个案例中,itreturnsthemultipliedresultofboththeobjectswithoutrounding。

在第一种情况下,返回两个对象的乘法结果而不四舍五入。

In the second case,itreturnsthemultipliedresultofboththeobjectswithroundingifrequired。

在第二种情况下,如果需要,使用舍入函数返回两个对象的乘法结果。

Example:

示例:

//javaprogramtodemonstratetheexample//of multiply () methodofbigdecimalimportjava.math.*; publicclassmultiplyofbd (publicstaticvoidmain (string args [ ] ) {//initialize three variables val 1,/val2and muls tal String mul_val='5'; //initializethreebigdecimalobjectsand//onemathcontextbigdecimalb _ de C1=new bigdecimal (val1); bigdecimal b _ de C2=new bigdecimal (val2); bigdecimal b _ de C3=new bigdecimal (mul _ val ); mathcontextma _ co=newmathcontext (2,RoundingMode.CEILING; system.out.println (多用途(bigdecimal ) : ); //multipliesthisbigdecimal (b _ de C2 ) by the//given bigdecimal (b _ de C3 ) andreturntheresultbigdecimalmul _ RES=b SSE System.out.println (' '; system.out.println (多点(二进制,多上下文) : ); //multipliesthisbigdecimal (b _ de C1 ) by the//given bigdecimal (b _ de C3 ) andreturntheresultbased//onthegivenconconce }} Output

出口量

多点(bigdecimal ) :b_deC2.multiply ) b_deC3 ) :20多点(bigdecimal ), MathContext ) :b_deC1. ma_co ) : 52是:3359 www.include help.com/Java/bigdecimal-multiply-method-co

二进制乘法

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。