使用java而不使用*运算符的十进制乘法

时间:2022-04-12 16:57:27

The below code works accurate on int

以下代码在int上运行准确

public class Example {
    public static void main(String[] args) {

        int a = 3, b = 3, mul = 0;

        // mul = a * b //this should not be used

        for (int i = 1; i <= a; i++)
            mul = mul + b;

        System.out.println(a + "*" + b + "-->" + mul);
    }
}

output : 3*3-->9 Now this is perfect

输出:3 * 3 - > 9现在这是完美的

But when i used double :

但是,当我使用双倍:

public class Example {
    public static void main(String[] args) {

        double a = 3.5, b = 3.5, mul = 0;

        // mul = a * b //this should not be used

        for (int i = 1; i <= a; i++)
            mul = mul + b;

        System.out.println(a + "*" + b + "-->" + mul);
    }
}

output:

3.5*3.5-->10.5 Now this is wrong since the correct answer of

3.5 * 3.5 - > 10.5现在这是错误的,因为正确的答案

3.5*3.5-->12.25

The problem is i need to multiply decimal values where as for loop wont support iteration over double value.

问题是我需要乘以十进制值,因为循环不支持迭代超过double值。

4 个解决方案

#1


0  

You can convert your decimal values to integer with divide by 0,(0..)1. After operation you should divide by 1(0..).

您可以将十进制值转换为除以0的整数,(0 ..)1。操作后,您应该除以1(0 ..)。

Ex: 3,5*3,5 = (3,5/0,1)*(3,5/0,1)/100 = 35*35/100 = 12,25

例:3,5 * 3,5 =(3,5 / 0,1)*(3,5 / 0,1)/ 100 = 35 * 35/100 = 12,25

Code;

 private double multiplication(double d1, double d2) {

        double divDiff1 = 1, divDiff2 = 1;

        while (d1 != Math.floor(d1)) {
            d1 = Double.valueOf(String.format("%.6f", d1 / 0.1));
            divDiff1 = divDiff1 / 0.1;
        }
        while (d2 != Math.floor(d2)) {
            d2 = Double.valueOf(String.format("%.6f", d2 / 0.1));
            divDiff2 = divDiff2 /0.1;
        }

        double mul = 0;
        for (int i = 1; i <= d1; i++)
            mul = mul + d2;

        return mul / divDiff2 / divDiff1;
    }

Call like below;

打电话如下;

multiplication(2.5,1.6);

#2


3  

Count decimal places, convert the numbers to integers, multiply via addition with loops, then reinsert your decimal place.

计算小数位数,将数字转换为整数,通过加法与循环相乘,然后重新插入小数位。

3.5 x 3.5 becomes 35 x 35, and then the decimal point is shifted 2 places to the left to give you your answer.

3.5 x 3.5变为35 x 35,然后小数点向左移动2位,为您提供答案。

#3


0  

The explanation of such behavior is explained in this post - Java Double Multiplication explanation?

这个行为的解释在这篇文章中解释了 - Java Double Multiplication的解释?

To resolve such issue it is best to use BigDecimal.

要解决此类问题,最好使用BigDecimal。

BigDecimal d1 = new BigDecimal("3.5");
BigDecimal d2 = new BigDecimal("3.5");
BigDecimal result = d1.multiply(d2);
System.out.printf("%s * %s = %s%n", d1, d2, result);

#4


0  

STEPS:

  • Ignore the decimal point and convert those numbers to integers
  • 忽略小数点并将这些数字转换为整数

  • Then Multiply them using additions (as you have done for integers)
  • 然后使用加法将它们相乘(就像你对整数所做的那样)

  • Convert the answer to string and place the decimal at its place.
  • 将答案转换为字符串并将小数放在其位置。

You will need String and StringBuilder for this.

您将需要String和StringBuilder。

Here is the code:

这是代码:

    double a = 3.5, b = 3.5;

    StringBuilder num1 = new StringBuilder(Double.toString(a));
    StringBuilder num2 = new StringBuilder(Double.toString(b));

    int indexOfDecimalPointFromBeginning_num1 = num1.indexOf(".");
    int indexOfDecimalPointFromBeginning_num2 = num2.indexOf(".");

    int decimalCounterNum1 = num1.length() - indexOfDecimalPointFromBeginning_num1 - 1;
    int decimalCounterNum2 = num2.length() - indexOfDecimalPointFromBeginning_num2 - 1;

    int decimalCountForResult = decimalCounterNum1 + decimalCounterNum2;

    String num1_withoutDecimal = num1.deleteCharAt(indexOfDecimalPointFromBeginning_num1).toString();
    String num2_withoutDecimal = num2.deleteCharAt(indexOfDecimalPointFromBeginning_num2).toString();

    int newNum1 = Integer.parseInt(num1_withoutDecimal);
    int newNum2 = Integer.parseInt(num2_withoutDecimal);

    int tempMul = 0;

    for (int i = 1; i <= newNum1; i++) {
        tempMul = tempMul + newNum2;
    }

    StringBuilder finalAnswer = new StringBuilder(Integer.toString(tempMul));
    finalAnswer.insert(finalAnswer.length() - decimalCountForResult, ".");

    Double finalAnswerInDouble = Double.parseDouble(finalAnswer.toString());
    System.out.println(a + "*" + b + " --> " + finalAnswer.toString());

#1


0  

You can convert your decimal values to integer with divide by 0,(0..)1. After operation you should divide by 1(0..).

您可以将十进制值转换为除以0的整数,(0 ..)1。操作后,您应该除以1(0 ..)。

Ex: 3,5*3,5 = (3,5/0,1)*(3,5/0,1)/100 = 35*35/100 = 12,25

例:3,5 * 3,5 =(3,5 / 0,1)*(3,5 / 0,1)/ 100 = 35 * 35/100 = 12,25

Code;

 private double multiplication(double d1, double d2) {

        double divDiff1 = 1, divDiff2 = 1;

        while (d1 != Math.floor(d1)) {
            d1 = Double.valueOf(String.format("%.6f", d1 / 0.1));
            divDiff1 = divDiff1 / 0.1;
        }
        while (d2 != Math.floor(d2)) {
            d2 = Double.valueOf(String.format("%.6f", d2 / 0.1));
            divDiff2 = divDiff2 /0.1;
        }

        double mul = 0;
        for (int i = 1; i <= d1; i++)
            mul = mul + d2;

        return mul / divDiff2 / divDiff1;
    }

Call like below;

打电话如下;

multiplication(2.5,1.6);

#2


3  

Count decimal places, convert the numbers to integers, multiply via addition with loops, then reinsert your decimal place.

计算小数位数,将数字转换为整数,通过加法与循环相乘,然后重新插入小数位。

3.5 x 3.5 becomes 35 x 35, and then the decimal point is shifted 2 places to the left to give you your answer.

3.5 x 3.5变为35 x 35,然后小数点向左移动2位,为您提供答案。

#3


0  

The explanation of such behavior is explained in this post - Java Double Multiplication explanation?

这个行为的解释在这篇文章中解释了 - Java Double Multiplication的解释?

To resolve such issue it is best to use BigDecimal.

要解决此类问题,最好使用BigDecimal。

BigDecimal d1 = new BigDecimal("3.5");
BigDecimal d2 = new BigDecimal("3.5");
BigDecimal result = d1.multiply(d2);
System.out.printf("%s * %s = %s%n", d1, d2, result);

#4


0  

STEPS:

  • Ignore the decimal point and convert those numbers to integers
  • 忽略小数点并将这些数字转换为整数

  • Then Multiply them using additions (as you have done for integers)
  • 然后使用加法将它们相乘(就像你对整数所做的那样)

  • Convert the answer to string and place the decimal at its place.
  • 将答案转换为字符串并将小数放在其位置。

You will need String and StringBuilder for this.

您将需要String和StringBuilder。

Here is the code:

这是代码:

    double a = 3.5, b = 3.5;

    StringBuilder num1 = new StringBuilder(Double.toString(a));
    StringBuilder num2 = new StringBuilder(Double.toString(b));

    int indexOfDecimalPointFromBeginning_num1 = num1.indexOf(".");
    int indexOfDecimalPointFromBeginning_num2 = num2.indexOf(".");

    int decimalCounterNum1 = num1.length() - indexOfDecimalPointFromBeginning_num1 - 1;
    int decimalCounterNum2 = num2.length() - indexOfDecimalPointFromBeginning_num2 - 1;

    int decimalCountForResult = decimalCounterNum1 + decimalCounterNum2;

    String num1_withoutDecimal = num1.deleteCharAt(indexOfDecimalPointFromBeginning_num1).toString();
    String num2_withoutDecimal = num2.deleteCharAt(indexOfDecimalPointFromBeginning_num2).toString();

    int newNum1 = Integer.parseInt(num1_withoutDecimal);
    int newNum2 = Integer.parseInt(num2_withoutDecimal);

    int tempMul = 0;

    for (int i = 1; i <= newNum1; i++) {
        tempMul = tempMul + newNum2;
    }

    StringBuilder finalAnswer = new StringBuilder(Integer.toString(tempMul));
    finalAnswer.insert(finalAnswer.length() - decimalCountForResult, ".");

    Double finalAnswerInDouble = Double.parseDouble(finalAnswer.toString());
    System.out.println(a + "*" + b + " --> " + finalAnswer.toString());