How to roundup a value

Questions about MultiCharts and user contributed studies.
arjfca
Posts: 1273
Joined: 23 Nov 2010
Has thanked: 721 times
Been thanked: 222 times

How to roundup a value

Postby arjfca » 28 Feb 2012

Hello

I know I had done it in the past, but now I forgot

I want to round up a number when the last 5' digit is greater than 0

1.34561 = 1.3457
1.34566 = 1.3457
1.34560 = 1.3456

I did try

Code: Select all

round( 1.34564,5):
but not the good result. The result is rounded low when the last digit is < 5

Martin

User avatar
Henry MultiСharts
Posts: 8503
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2731 times

Re: How to roundup a value

Postby Henry MultiСharts » 29 Feb 2012

Hello Martin,

There are multiple code words to round the value in the PowerLanguage: Round, Floor, Ceiling.
Unfortunately there is no code word to round low when the last digit is < 5.
You need to write your own rounding function to achieve your goal.
These users thanked the author Henry MultiСharts for the post:
JoshM

User avatar
TJ
Posts: 6584
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 970 times
Been thanked: 1907 times

Re: How to roundup a value

Postby TJ » 29 Feb 2012

arjfca wrote:Hello

I know I had done it in the past, but now I forgot

I want to round up a number when the last 5' digit is greater than 0

1.34561 = 1.3457
1.34566 = 1.3457
1.34560 = 1.3456

I did try

Code: Select all

round( 1.34564,5):
but not the good result. The result is rounded low when the last digit is < 5

Martin

just add 0.0004 to the number before rounding.
These users thanked the author TJ for the post (total 3):
arjfcaJoshMLobo.Trader

arjfca
Posts: 1273
Joined: 23 Nov 2010
Has thanked: 721 times
Been thanked: 222 times

Re: How to roundup a value

Postby arjfca » 29 Feb 2012

NIce, clever, inteligent.... Could I say more :)

I had came out with a solution with
Inputs: ValueNumber(Numeric), Lastdigit(Numeric);
//OneMorePip (TrueFalse);

Variables: PowerValue(1);

PowerValue = ( power(10, Lastdigit)); // 10 to power 1 = 10, power2 = 100, Power 3 = 1000, power 4 = 10000

Value2 = Valuenumber *PowerValue; //Multiply the fraction number to the 10' power Value. 1.23456 X (10 power 4) = 1.2345.6
//The result is a bigger number with the least significant vnumber is after the " ."
Value3 =ceiling(Value2); // The least significant fraction is raise to the first integer 12 345.6= 12 346.00

PowerValue = ( power(10, (Lastdigit)*-1)); //The result is then put back tio the original level 12 346 X (10 power(-4) = 1.2346

MT_RoundUp = value3 *PowerValue;


One line for you.... Add .0004 to the value... wow!

Another Sandwich
You will need friends to eat all those Montreal Smoke Meat Sandwiches that I owe you

Martin
These users thanked the author arjfca for the post (total 2):
TJNW27

User avatar
TJ
Posts: 6584
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 970 times
Been thanked: 1907 times

Re: How to roundup a value

Postby TJ » 29 Feb 2012

Thanks for another famous smoked meat sandwich.

I like one-line solutions.

Here's my favorite:
viewtopic.php?f=1&t=9164&p=41489#p41489
These users thanked the author TJ for the post (total 2):
JoshMNW27

NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

Re: How to roundup a value

Postby NW27 » 29 Feb 2012

Hi TJ,

Nice solutions to both questions.

Neil.


Return to “MultiCharts”