Dividing integers and remainders
Does anyone know how to only get the first two digits of a remainder?
EXAMPLE
int var1 = 0;
int var2 = 244;
var1 = var2 %120;
this will return .0333333333333333
I want to just get .03 everytime and always have the first two digits. Is there a way to set the precision or something so that I can always only have 2 digits?
[375 byte] By [
hoagers34] at [2007-11-18 1:38:01]

# 1 Re: Dividing integers and remainders
I would really like to se an integer (var1) containing a fractional number :D
# 2 Re: Dividing integers and remainders
Heh, no kidding. And 244 % 120 is 4.
mje at 2007-11-10 8:54:58 >

# 3 Re: Dividing integers and remainders
Quite clearly you are a lunatic if you think an integer can
contain .0333333333333333! :-)
Adrian
# 4 Re: Dividing integers and remainders
You confused everybody with your use of the "modulo" operator. That, and you have a fuzzy concept of the "integer" data type. Try this:
int var1 = 120;
int var2 = 244;
int quotient = var2 / var1;
int remainder_times_100 = (100 * (var2 - var1 * quotient)) / var1;
The value of "remainder_times_100" will be the first two digits of the remainder expressed as a decimal fraction.
TSYS at 2007-11-10 8:57:02 >

# 5 Re: Dividing integers and remainders
Using the following program, I get:
2 3
2 4
#include <iostream>
using namespace std;
void Function1(int var1, int var2) {
int quotient = var2 / var1;
int remainder_times_100 = (100 * (var2 - var1 * quotient)) / var1;
cout << quotient << ' ' << remainder_times_100 << '\n';
}
void Function2(int var1, int var2) {
div_t Div = div(var2, var1);
cout << Div.quot << ' ' << Div.rem << '\n';
}
int main(int argc, char *argv[], char *envp[]) {
Function1(120, 244);
Function2(120, 244);
return 0;
}
# 6 Re: Dividing integers and remainders
try this --
int a = 4;
int b = 3;
float q = 0;
int round;
q = (float)a/b;
q *= 100;
round = q;
q = (float)round/100;
# 7 Re: Dividing integers and remainders
As far as I know, floating-point is unreliable for things like this.
# 8 Re: Dividing integers and remainders
while it might be unreliable i think that pppsandeep is just showing how to isolate the 2 numbers the original poster wanted and for that it shouldn't cause a problem.
# 9 Re: Dividing integers and remainders
Sam:
Your Function2 returns the remainder of the division, which wasn't the question as I understood it. My understanding was that "hoagers34" wanted the first two digits of the fractional part of the division, ie., the "03" of ".03333333".
Once again I have to ask: Why isn't the original questioner taking part in this discussion?
TSYS at 2007-11-10 9:02:11 >

# 10 Re: Dividing integers and remainders
Yes. Robert, I misunderstood.
# 11 Re: Dividing integers and remainders
I would like to know why hoagers34 wishes to perform this operation.
Is it so that further calculations can be made using the adjusted figure, say for currency calculations, or is it purely to make the value display properly in a text box?
If it's the first case, I would recommend working in pennies rather pounds/dollars/etc and then you only need to work with whole numbers.
If it's for display reasons, use whatever formatting operators your output method has for limiting the displayed value to 2 decimal places.
Either way, it's quicker and simpler than trying to manipulate a floating point variable.
# 12 Re: Dividing integers and remainders
I sure hope this isn't for some sort of currency calculation - the round-off errors would quickly accumulate to serious money. But in the absence of any further input from hoagers34, one can only assume it was an idle question of little import.
TSYS at 2007-11-10 9:05:17 >

# 13 Re: Dividing integers and remainders
Many of the threads that grow beyond reason are those in which the creator of the thread does not participate after creating it. Without the participation of hoagers34 I am not interested in speculation and will simply unsubscribe from this thread.