ShowAll Questionssorted byDate Posted

# Long variable misses a digit in division

I am writing a validation code that checks if the module and division of the module of another meet some criteria:
```Long decVal = Long.ValueOf(tmp);
Integer reminder = Modulo529(decVal);
Integer cociente = reminder/23;
Integer residuo = math.mod(reminder,23);

public Integer Modulo529(Long x){
Long result;
Long div = 529;
result = (x/div);
result = x - (529 * result);
return Integer.valueOf(result);
}```
My problem is that given some number (0987543210987654) the result of the module is wrong, the simple division of that number should be 1866811362925, instead it is 186681136292, it loses the last digit (5), it should be inside the range of 2^63 - 1.
Could you help me?
Thank you very much.
Manish Bhati
Hi Buddy,

The Problem is not with Long, but you are retrieving wrong value and you didn't identify it:-

See line No.2 I have changed it to (2,18)
```String CUPS = '120987543210987654';
String tmp = CUPS.substring(2,18);
System.debug('Value for division containg 4 in the end :- '+tmp);
Long decVal = Long.ValueOf(tmp);
Integer reminder = Modulo529(decVal);
Integer cociente = reminder/23;
Integer residuo = math.mod(reminder,23);

public Integer Modulo529(Long x){
Long result;
Long div = 529;
result = (x/div);
system.debug('Value for x '+x);
system.debug('result which was not having 5 before '+result);
result = x - (529 * result);
return Integer.valueOf(result);
}```

In your case the division was happening with 098754321098765 not with 0987543210987654 (ending with 4).
The division of 098754321098765 / 529 = 186681136292 (So the value was coming correct).
while the division of 0987543210987654 / 529 = 1866811362925.

Just print your tmp value before -  Long decVal = Long.ValueOf(tmp);
System.debug(tmp);
you will come to know that the value you were passing was 098754321098765 not 0987543210987654

Hope it explains.

Manish Bhati
Hey,
It is working fine for me.
​Just pass the value as 0987543210987654L otherwise it will treat your value as Integer.(As we does in Java :) )
```Long decVal = 0987543210987654L;
Integer reminder = Modulo529(decVal);
Integer cociente = reminder/23;
Integer residuo = math.mod(reminder,23);

public Integer Modulo529(Long x){
Long result;
Long div = 529;
result = (x/div);
system.debug(result);
result = x - (529 * result);
system.debug(Integer.valueOf(result));
return Integer.valueOf(result);
}```

Hey Manish Bhati, it works if the variable is entered manually, but I am trying to get the Long variable value from a cast of a String, I tried adding the sufix doing +'L', and it does not work, do you know how to work around that?
```String tmp = CUPS.substring(2,17)+'L';
Long decVal = Long.ValueOf(tmp);
Integer reminder = Modulo529(decVal);```

Manish Bhati
Hi Buddy,

The Problem is not with Long, but you are retrieving wrong value and you didn't identify it:-

See line No.2 I have changed it to (2,18)
```String CUPS = '120987543210987654';
String tmp = CUPS.substring(2,18);
System.debug('Value for division containg 4 in the end :- '+tmp);
Long decVal = Long.ValueOf(tmp);
Integer reminder = Modulo529(decVal);
Integer cociente = reminder/23;
Integer residuo = math.mod(reminder,23);

public Integer Modulo529(Long x){
Long result;
Long div = 529;
result = (x/div);
system.debug('Value for x '+x);
system.debug('result which was not having 5 before '+result);
result = x - (529 * result);
return Integer.valueOf(result);
}```

In your case the division was happening with 098754321098765 not with 0987543210987654 (ending with 4).
The division of 098754321098765 / 529 = 186681136292 (So the value was coming correct).
while the division of 0987543210987654 / 529 = 1866811362925.

Just print your tmp value before -  Long decVal = Long.ValueOf(tmp);
System.debug(tmp);
you will come to know that the value you were passing was 098754321098765 not 0987543210987654

Hope it explains.
This was selected as the best answer