Jump to content

rounding numbers


ehigginbotham

Recommended Posts

I need to round off a number.

example, my number is 41.25

I want to multiply it by 1.25, and then round it always up to the next 5 increment.

 

so

41.25 x 1.25 = 51.56

I need that number to come out to be 55.

I have figured out how to round it, it just doesn't always round up.

here is the final rule

var x = parseInt(Rule("g1"))

var mult = (x*1.25)

 

var z = Math.round(mult / 5) *5

var y = parseInt(z)

return y;

 

 

here is rule G1

var x = parseFloat(Field("AMT"))

var z = Math.round(x)

var y = parseInt(z)

return y;

 

any ideas to make it always round up to the nearest 5 increment in whole numbers?

Link to comment
Share on other sites

As noted in this thread:

Math.ceil rounds up, Math.floor rounds down and Math.round rounds to the nearest integer.

So, you want to use Math.ceil:

var x = parseInt(Rule("g1"))
var mult = (x*1.25)

var z = Math.[color="Red"]ceil[/color](mult / 5) *5 
var y = parseInt(z)
return y;

 

That said, there is a little bit of redundancy in your code. In rule G1, why parseFloat (keeping decimals), then round to the nearest integer (drop decimals), and then parseInt (convert an integer to an integer)?

 

It might be cleaner/easier to read if you just did something like this:

return Math.ceil(Field("AMT") * 1.25 / 5) * 5;

Edited by step
grammar
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...