How to adjust Price value

I have the following code:

tmp = sL - 0.273*ht;

It returns 151.672

Now I would like it rounded to 151.65 since the decimal increments by 5 units in my country.

So, to make it clear:
If the returned value is 151.672, it should be rounded as 151.65
If the returned value is 151.678, it should be rounded as 151.70

In the above example, 151.675 is the cutoff. If the returned value is below the cutoff, it is rounded as 151.65 and above cutoff, it is rounded as 151.70

Could somebody help me, how to code this?

Thank you.

Here's some pseudo code to try out:

// tmp is the number to process (151.672 or 151.70 or...)
rem = mod(tmp, 0.05) // calculates remainder

if rem < 0.25, 
  num_rnd = tmp - rem
  num_rnd = tmp + (0.05-rem)

So when tmp= 151.678, rem = 0.028, and for 151.672, rem = 0.022. When you plug this into the if statement, you get your rounded number to the 0.05 level.

Hope this helps you get started

Wow. Thank you very much. I didn't know the mode function existed.


Did a quick search in the AFL function list. I couldn't find the mod function.

Does anybody know?

@reachsomu, you can ask @rocketPower directly, because the code he has posted is clearly not AFL.

1 Like

@reachsomu, "mod" in Amibroker is the Modulus (%) operator.

See, for instance, this kb article to see how it can be used for another purpose.

The list of operators used by Amibroker is found on this manual page.

1 Like

Here's another way to solve the problem:

tmp = sL - 0.273*ht;
pRounded = round(tmp /0.05) * 0.05;
1 Like

Yeah sorry I stated it was pseudocode, I didn't have time to post code that I could check myself in AB, but wanted to describe the concept.