?
Solved

Divide and get remainder

Posted on 2001-06-08
15
Medium Priority
?
800 Views
Last Modified: 2007-12-19
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
Comment
Question by:CUTTHEMUSIC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +4
15 Comments
 

Expert Comment

by:HabBoy
ID: 6168974
with vbscript you would use the mod operator

eg. 10 mod 6  ' returns 4
0
 
LVL 2

Author Comment

by:CUTTHEMUSIC
ID: 6168986
<%
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
 
LVL 2

Author Comment

by:CUTTHEMUSIC
ID: 6168988
(rounding floating-point numbers to integers) <-- not what I want
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:HabBoy
ID: 6169000
Have you tried '/'?
0
 

Expert Comment

by:HabBoy
ID: 6169003
Have you tried '/'?
0
 
LVL 4

Accepted Solution

by:
aponcealbuerne earned 100 total points
ID: 6169026
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
 
LVL 2

Author Comment

by:CUTTHEMUSIC
ID: 6169157
aponcealbuerne
seems like it might work. I'll try it in my application and see if it gives good results.
0
 
LVL 4

Expert Comment

by:DMN
ID: 6169179
<%=2001.99-CInt(2001.99/1000)*1000%>

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

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

Expert Comment

by:mgfranz
ID: 6169400
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
 
LVL 3

Expert Comment

by:Hornet241
ID: 6169990

Try this


str1 = 2001.99
str2 = 1000

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

Expert Comment

by:weesiong
ID: 6170328
Hornet241 formula is correct ;p

Regards,
Wee Siong
0
 
LVL 4

Expert Comment

by:DMN
ID: 6170443
Hornet241 formula is same as my shown above..
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6170490
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
 
LVL 3

Expert Comment

by:Hornet241
ID: 6171677
DMN

I miss that too,

Sorry didn't mean to copy your comment
0
 
LVL 4

Expert Comment

by:DMN
ID: 6172888
:) Thanx
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question