Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

2 Decimal Places without Rounding

fikrizahari
8 - Asteroid

Hi,

Good day !

I would like to 'round down' numbers to 2 decimal places.
Example as below :

Field1   Correct Output  
30689.7130689.71
4300.2864300.28
6504.6616504.66
6074.7286074.72
467.1111467.11
2222.2492222.24
2277.222277.22
2273.72273.7
2277.222277.22

 


However, i could not find 'round down' formula in Alteryx, so I use 'floor' formula.
Formula for 'Round Down' column that i used, and the result as below :

fikrizahari_3-1678849339764.png

 


You can see there some different expected output on Round Down column ( Record 7 - 9 ).
Please advise what is the reason, or is there any better formula to get 2 decimal places without rounding it.

Thanks !

11 REPLIES 11
ShankerV
17 - Castor

Hi @fikrizahari 

 

One way of doing this.

 

ShankerV_0-1678850247669.png

 

Many thanks

Shanker V

ShankerV
17 - Castor

Hi @fikrizahari 

 

Step 1: Input

ShankerV_0-1678850334211.png

 

Step 2: Change to String format

ShankerV_1-1678850357031.png

ShankerV_2-1678850364773.png

 

Step 3: 

ShankerV_3-1678850390779.png

(\d+\.\d{0,2})

 

ShankerV_5-1678850406422.png

 

Step 4: Change back to Numeric datatype.

 

ShankerV_6-1678850446594.png

 

ShankerV_7-1678850457353.png

 

Many thanks

Shanker V

fikrizahari
8 - Asteroid

Hi @ShankerV,

Thank you for the solution.
It working so well.

However, is the expression : (\d+\.\d{0,2}) can be input in Formula tools ?
This due to, there a lot of other formula that link to this question.

I tried this :
REGEX_Replace([Field1], "(\d+\.\d{0,2})","$1")

but it isnt working :

fikrizahari_0-1678851838581.png

 

Thanks !

ShankerV
17 - Castor

Hi @fikrizahari 

 

Even I tried the same in Regex tool using the Replace function.

 

Output if the Copy unmatched text to output is enabled.

ShankerV_0-1678853591849.png

 

 

Difference in output if it is unchecked.

 

ShankerV_1-1678853647301.png

 

So you need to inherit this in your formula tool when using the Regex replace as a function.

 

Many thanks

Shanker V

Yoshiro_Fujimori
15 - Aurora

@fikrizahari , @ShankerV 

I understand the diff is caused by the rounding error of Double data type.

(because we want to display in decimal number but the computer stores it in binary number.)

 

So, another way to avoid this error might be shifting the boundary a little bit as below.

FLOOR([Field1]*100 + 0.1)/100

 

Result:

Yoshiro_Fujimori_1-1678854419871.png

 

 

RobertOdera
13 - Pulsar

Hi, @fikrizahari 

 

Here you go.

Please mark it as an acceptable solution if it works for you.

 

RobertOdera_0-1678910060378.png

 

fikrizahari
8 - Asteroid

@Yoshiro_Fujimori

Thank you for clarification and solution.
It works well with just a little shifting.


Hi@ShankerV & @RobertOdera

The solution look great and give the same output as well.
Thank you so much !

fikrizahari
8 - Asteroid

Hi @Yoshiro_Fujimori and everyone,

I have another question, how do i Round Up to next 0.05.


Field1   Require Output   
1.21.2
1.211.25
1.221.25
1.231.25
1.241.25
1.251.25
1.261.3
1.271.3
1.281.3
1.291.3
1.31.3


Currently I'm using Round([Field1],0.05) but it goes to the nearest 0.05. ( which is wrong )

fikrizahari_0-1678947127472.png


Thanks !

ShankerV
17 - Castor

Hi @fikrizahari 

 

One way of doing this.

 

IF [Field1]-floor([Field1]) in (0,0.05)
THEN [Field1]
ELSE
ROUND([Field1]+.025,0.05)
ENDIF

 

ShankerV_0-1678947566334.png

ShankerV_1-1678947597593.png

 

Many thanks

Shanker V

Labels