求大神,在线求救啊
解决方案 »
- 谁能帮我解释一下定义泛型下界时,使用getSuperclass()不能编译?
- JPA OneToMany 映射问题
- Java 是如何导入 rt.jar 这个包的呢?
- 关于Integer
- 对一条Java运行命令的疑惑
- 页面的加了这个上传的enctype="multipart/form-data,怎么取字段的数组值,不能request.getParameterValues(""),急!!!
- J显示柱状图?
- 问题 3.3.6.24()有什么规律。后面写一个数
- BorderLayout的各元素宽度可以设置吗?
- 用java通过pop3怎样得到附件?
- CardLayout的一些问题,求解决
- java值传递的问题
加 max(addend.scale(), augend.scale())
减 max(minuend.scale(), subtrahend.scale())
乘 multiplier.scale() + multiplicand.scale()
除 dividend.scale() - divisor.scale()
这些标度是返回准确算术结果的方法使用的标度;准确相除可能必须使用较大的标度除外,因为准确的结果可能有较多的位数。例如,1/32 得到 0.03125。 舍入之前,逻辑的准确中间结果的标度是该运算的首选标度。如果用 precision 位数无法表示准确的数值结果,则舍入会选择要返回的一组数字,并将该结果的标度从中间结果的标度减小到可以表示实际返回的 precision 位数的最小标度。如果准确结果可以使用最多 precision 个数字表示,则返回具有最接近首选标度的标度的结果表示形式。尤其是,通过移除结尾零并减少标度,可以用少于 precision 个数字来表示准确的可表示的商。例如,使用 floor 舍入模式将结果舍入为三个数字,
19/100 = 0.19 // integer=19, scale=2
但是
21/110 = 0.190 // integer=190, scale=3
注意,对于加、减和乘,标度的缩减量将等于丢弃的准确结果的数字位置数。如果舍入导致进位传播创建一个新的高位,则当未创建新的数位时,会丢弃该结果的附加数字。 其他方法可能与舍入语义稍微不同。例如,使用指定的算法的 pow 方法得到的结果可能偶尔不同于舍入得到的算术结果,如最后一位有多个单位(ulp)。 可以通过两种类型的操作来处理 BigDecimal 的标度:标度/舍入操作和小数点移动操作。标度/舍入操作(setScale 和 round)返回 BigDecimal,其值近似地(或精确地)等于操作数的值,但是其标度或精度是指定的值;即:它们会增加或减少对其值具有最小影响的存储数的精度。小数点移动操作(movePointLeft 和 movePointRight)返回从操作数创建的 BigDecimal,创建的方法是按指定方向将小数点移动一个指定距离。 为了简洁明了起见,整个 BigDecimal 方法的描述中都使用了伪代码。伪代码表达式 (i + j) 是“其值为 BigDecimal i 加 BigDecimal j 的 BigDecimal”的简写。伪代码表达式 (i == j) 是“当且仅当 BigDecimal i 表示与 BigDecimal j 相同的值时,则为 true”的简写。可以类似地解释其他伪代码表达式。方括号用于表示特定的 BigInteger 和定义 BigDecimal 值的标度对;例如,[19, 2] 表示 BigDecimal 在数值上等于 0.19,标度是 2。 注:如果 BigDecimal 对象用作 SortedMap 中的键或 SortedSet 中的元素,则应特别小心,因为 BigDecimal 的自然排序与 equals 方法不一致。有关更多信息,请参见 Comparable、SortedMap 或 SortedSet。 当为任何输入参数传递 null 对象引用时,此类的所有方法和构造方法都将抛出 NullPointerException。
import java.math.BigDecimal;class Multiply
{
public static double mul(double d1, double d2)
{ // 进行乘法运算
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.multiply(b2).doubleValue();
}
public static double round(double d,
int len)
{
// 进行四舍五入操作 BigDecimal b1 = new BigDecimal(d);
BigDecimal b2 = new BigDecimal(1);
// 任何一个数字除以1都是原数字
// ROUND_HALF_UP是BigDecimal的一个常量, return b1.divide(b2, len,BigDecimal.
ROUND_HALF_UP).doubleValue();
}}
public class TestMultiply
{
public static void main(String [] args)
{
System.out.println("乘法运算:" +
Multiply.round(Multiply.mul(10.345,
3.333), 3));}
}
程序编译时报错,我刚学JAVA,请7楼改一下
import java.util.ArrayList;
import java.util.List;public class TimesTest { public static void main(String[] args) {
String s1 = "123456789";
String s2 = "123456789";
String result = multiplyStringByString(s1, s2);
System.out.println(result);
} /**
* 获取两个字符串的乘积
* @param s1 String
* @param s2 String
* @return String
*/
public static String multiplyStringByString(String s1, String s2) {
List<Integer> resultList = null;
for (int i = s1.length() - 1, j = 0; i >= 0; i--, j++) {
char c = s1.charAt(i);
List<Integer> numList = multiplyCharByString(c, s2);
resultList = SumOfTwoList(resultList, numList, j);
}
StringBuffer sb = new StringBuffer();
for (int i = resultList.size() - 1; i >= 0; i--) {
sb.append(resultList.get(i));
}
return sb.toString();
} /**
* 计算两个数组的和
*
* @param resultList
* 存放结果的数组
* @param numList
* 待加数组
* @param bit
* 从第几位开始加
* @return List<Integer>
*/
public static List<Integer> SumOfTwoList(List<Integer> resultList,
List<Integer> numList, int bit) {
if (resultList == null) {
return numList;
}
for (int i = 0, j = bit; i < numList.size(); i++, j++) {
if (j < resultList.size()) {
resultList.set(j, resultList.get(j) + numList.get(i));
} else {
resultList.add(numList.get(i));
}
}
carryBit(resultList);
return resultList;
} /**
* 获取字符串乘以字符的结果
*
* @param c
* char
* @param s
* String
* @return String
*/
public static List<Integer> multiplyCharByString(char c, String s) {
int[] integers = convertStringToInteger(s);
int num = convertCharToInteger(c);
List<Integer> resultList = new ArrayList<Integer>();
for (int i = integers.length - 1; i >= 0; i--) {
int result = num * integers[i];
resultList.add(result);
}
carryBit(resultList);
return resultList;
} /**
* 进位
*
* @param resultList
* List<Integer>
*/
private static void carryBit(List<Integer> resultList) {
for (int i = 0; i < resultList.size(); i++) {
int result = resultList.get(i);
if (result / 10 > 0) {
resultList.set(i, result % 10);
if (resultList.size() > i + 1) {
resultList.set(i + 1, resultList.get(i + 1) + result / 10);
} else {
resultList.add(result / 10);
}
}
}
} /**
* 将字符串转换为整形数组
*
* @param s
* String
* @return int[]
*/
public static int[] convertStringToInteger(String s) {
int[] integers = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
integers[i] = convertCharToInteger(s.charAt(i));
}
return integers;
} /**
* 将字符转换成整数
*
* @param c
* char
* @return int
*/
public static int convertCharToInteger(char c) {
return (int) (c) - 48;
}
}求两个字符串的乘积,写的乱点...