Jump to content

Multiply result of rounding


Recommended Posts

I've created this rule which fills my first need. Now I need to take the result of this and multiply by 1.5

 

if (StringToNumber(Field("GIFTAMNT")) < 20.01)

{

return "<span>" + String("20") + "</span>";

}

if (StringToNumber(Field("GIFTAMNT")) > 500)

{

return "<span>" + String("500") + "</span>";

}

else

{

return "<span>" + (Math.ceil(Field("GIFTAMNT")/5)*5) + "</span>";

}

return "";

Link to comment
Share on other sites

I've created this rule which fills my first need. Now I need to take the result of this and multiply by 1.5

 

if (StringToNumber(Field("GIFTAMNT")) < 20.01)

{

return "<span>" + String("20") + "</span>";

}

if (StringToNumber(Field("GIFTAMNT")) > 500)

{

return "<span>" + String("500") + "</span>";

}

else

{

return "<span>" + (Math.ceil(Field("GIFTAMNT")/5)*5) + "</span>";

}

return "";

All those <span> tags do nothing. I would start by getting rid of them, and then write the rule like so:

var num = StringToNumber(Field("GIFTAMNT"));
var result = 0;

if (num  < 20.01)
   result = 20;
else if (num > 500)
   result = 500;
else
   result = Math.ceil(num/5)*5;

return result * 1.5;

Or, more succinctly:

return Math.ceil(Math.max(20, Math.min(500, StringToNumber(Field("GIFTAMNT"))))/5)*7.5;

Link to comment
Share on other sites

Thanks Dan, This does exactly what I needed.

 

Curious, if the <span> tag does nothing why does the rule wizard put them in when you convert to javascript?

The Drag-and-Drop Rule Wizard puts in those tags because it *might* be changing the font, or some other style attribute of the text, in which case there will be other formatting tags directly following the <span> tag. But if there are no style changes, it's basically superfluous. I suppose the "Convert to JavaScript" logic could be changed to not bother putting out an empty <span> tag when there's no style change, but that Drag-and-Drop editor was not designed with the goal of producing simple converted JavaScript code. In any case, if you're simply doing a basic "if-else" chain and returning plain text without any formatting tags, I think it's just as easy to write the JavaScript code as to use that Drag-and-Drop GUI, especially if you're already familiar enough with JavaScript to be calling out Math functions.

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...