• Status: Solved
• Priority: Medium
• Security: Public
• Views: 819

# Divide and get remainder

How do I divide one number by another and get a remainder if there is one.
ex.   2001.99 / 1000  would give me 2 remainder 1.99
0
CUTTHEMUSIC
• 3
• 3
• 3
• +4
1 Solution

Commented:
with vbscript you would use the mod operator

eg. 10 mod 6  ' returns 4
0

Author Commented:
<%
Dim strValue1, strValue2
strValue1 = 2001.99
strValue2 = 1000
Response.Write strValue1 Mod strValue2
%>

Try it. It gives you 2 I'm looking for 1.99

Here is what MSDN says about mod

Mod Operator
Divides two numbers and returns only the remainder.

result = number1 Mod number2

Arguments
result

Any numeric variable.

number1

Any numeric expression.

number2

Any numeric expression.

Remarks
The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (which is result) equals 5.

A = 19 Mod 6.7
0

Author Commented:
(rounding floating-point numbers to integers) <-- not what I want
0

Commented:
Have you tried '/'?
0

Commented:
Have you tried '/'?
0

Commented:
Maybe you could do something like:

strValue1 = 2001.99 * 100
strValue2 = 1000
strValue3 = strValue1 Mod strValue2
and then use the strValue3 divided / 100

hope helps
0

Author Commented:
aponcealbuerne
seems like it might work. I'll try it in my application and see if it gives good results.
0

Commented:
<%=2001.99-CInt(2001.99/1000)*1000%>

In common case, to find remainder for A and B write:

<%=A-CInt(A/B)*B%>
0

Commented:
Try a Round();

NumDecimalPlaces

The optional NumDecimalPlaces argument specifies how many decimal places to round off to.

Code:
<% =Round(1.123456789, 6) %>

Output:
1.123457

Note that negative numbers are rounded down (more negative).

Code:
<% =Round(-2.899999999, 2) %>

Output:
-2.9

<%
strValue1 = 2001.99
strValue2 = 1000
newStr = Round((strValue1 Mod strValue2), 3)
Response.Write newStr
%>

UNTESTED

0

Commented:

Try this

str1 = 2001.99
str2 = 1000

Remainder = Str1 - (Str2 * Int(str1 / str2))
0

Commented:
Hornet241 formula is correct ;p

Regards,
Wee Siong
0

Commented:
Hornet241 formula is same as my shown above..
0

Commented:
DMN,

Ya, sorry, i miss it :p

I also make a function before, but after see your formula, i believe your formula more faster and good then me :p

Function FormatNumberDemo(NumberIn, ModNumber)

FormatNumberDemo = NumberIn
Do While FormatNumberDemo > ModNumber
FormatNumberDemo = FormatNumberDemo - ModNumber
Loop
End Function

Response.Write(FormatNumberDemo(2001.99, 1000))

Regards,
Wee Siong
0

Commented:
DMN

I miss that too,

Sorry didn't mean to copy your comment
0

Commented:
:) Thanx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.