UserJS
UserJS

Reputation: 131

Multiplication of numbers as string

Why is '0' subtracted from n1.charAt(i) and n2.charAt(j)?

Question: Given two numbers represented as strings, return multiplication of the numbers as a string.

public String multiply(String num1, String num2) {
    String n1 = new StringBuilder(num1).reverse().toString();
    String n2 = new StringBuilder(num2).reverse().toString();

    int[] d = new int[num1.length()+num2.length()];

    //multiply each digit and sum at the corresponding positions
    for(int i=0; i<n1.length(); i++){
        for(int j=0; j<n2.length(); j++){
            d[i+j] += (n1.charAt(i)-'0') * (n2.charAt(j)-'0');
        }
    }

    StringBuilder sb = new StringBuilder();

    //calculate each digit
    for(int i=0; i<d.length; i++){
        int mod = d[i]%10;
        int carry = d[i]/10;
        if(i+1<d.length){
            d[i+1] += carry;
        }
        sb.insert(0, mod);
    }

    //remove front 0's
    while(sb.charAt(0) == '0' && sb.length()> 1){
        sb.deleteCharAt(0);
    }

    return sb.toString();
}

Upvotes: 2

Views: 816

Answers (4)

Elliott Frisch
Elliott Frisch

Reputation: 201537

It's calculating (by subtraction) the numeric value of the character. The character constant 0 is also digit 0. Consider a loop of the char value '0' to '9' can also be done as an int.

for (char ch = '0'; ch <= '9'; ch++) {
    System.out.print(ch);
    System.out.print(" = " );
    System.out.println((int) ch);
}

or

for (int ch = '0'; ch <= '9'; ch++) {
    System.out.print((char) ch);
    System.out.print(" = " );
    System.out.println(ch);
}

which (both) demonstrate the ascii code (technically Unicdoe, but the technique predates Unicode) values for the digits

0 = 48
1 = 49
2 = 50
3 = 51
4 = 52
5 = 53
6 = 54
7 = 55
8 = 56
9 = 57

See also Character.digit(char, int) which returns the numeric value of the character ch in the specified radix. Which means you could replace

d[i + j] += (n1.charAt(i) - '0') * (n2.charAt(j) - '0');

with

d[i + j] += Character.digit(n1.charAt(i), 10)
    * Character.digit(n2.charAt(j), 10);

of course, the method could be implemented with a call to the Java built-in arbitrary precision type BigInteger and you could use BigInteger.multiply(BigInteger) like

return new BigInteger(num1).multiply(new BigInteger(num2)).toString();

Upvotes: 3

Saif
Saif

Reputation: 7052

When a character is stored in an integer it is converted to it's corresponding ASCII value stored.

Now the ASCII value of:

'0' (zero as character) is -> 48   
'1' (one as character ) is -> 49
 ......................
 and so on...

Now, how can we get the integer out of the character ?

Its easy to subtract 48 from all numeric character and which is ASCII of '0'

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504172

Why is '0' subtracted from n1.charAt(i) and n2.charAt(j) ?

Because the number of the Unicode character representing the digit 0 is 48, not 0.

Imagine you wanted to label 'A' => 0, 'B' => 1 etc... then you'd use

n1.charAt(i) - 'A'

etc... but as it happens, we want '0' => 0, '1' => 1 etc, so we subtract '0'. You need to differentiate between "the value, as an int" and "the character, as a char" (where the latter still has a number value, but it's not what you might expect it to be).

Upvotes: 2

Eran
Eran

Reputation: 394156

Subtracting '0' from a character that contains a digit gives you the numeric value of that character.

For example, '9' - '0' gives you the integer 9.

The reason for that is that the digit characters ('0' to '9') have integer values from 48 to 57, so '9' - '0' is equivalent to 57 - 48, which gives you 9.

Upvotes: 2

Related Questions