Solved

Divide and get remainder

Posted on 2001-06-08
15
789 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
  • 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
 

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 25 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
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 video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now