# Thread: Using function in a calculation problems

1. ## Using function in a calculation problems

Hello,
I'm creating an application that help Fashion designer to calculate data when crating a sloper for the garment, in one of the fields is needed the following calculation,
Example:
Bust = 33"
Underbust = 27"
A: Cup size = If the underbust is an ODD number add 5", and if the result is even then add 4" in our example the number is 27" so we add 5" the result then is 32".
New Underbust number is 32"
B:next: Bust - New UnderBust 33"-32"= 1"
If the result = 1" the cup size is "A"
If the result = 2" the cup size is "B"
If the result = 2" the cup size is "B"
In our case the Cup Size is "A"
In excel is very simple:
A: =IF(ISODD(UnderBust),UnderBust+5",UnderBust+4")
B: =IF((Bust-UnderBust)=1,"A",IF((Bust-UnderBust)=2,"B","C"))

In
Alpha anywhere v 12 the ISODD function is not recognize.
Can anybody Help me fix this issue?

Thanks
Samuel Eskenasy

2. ## Re: Using function in a calculation problems

Use Mod() to determine if it is even or odd.

3. ## Re: Using function in a calculation problems

? mod(int(-1),2)
= -1
? mod(int(2.5),2)
= 0
? mod(int(5),2)
= 1
So if mod(int(some_value),2) <> 0 the number ISODD.

4. ## Re: Using function in a calculation problems

See Stan's solution to this here:

http://www.alphasoftware.com/alphafo...ght=odd+number

Edit: Geeze, where else can you get this much help this fast?

5. ## Re: Using function in a calculation problems

Thanks for the citation, Steve. Seems the Excel ISODD() operates a bit differently with fractional parts so I tweaked the logic a bit.

6. ## Re: Using function in a calculation problems

i see you are in mobile & Browser forum, where is this calculation being done
in an ux or an updatable grid? if so, then you may want to consider writing the code in javascript. which will not require a callback to the server.

7. ## Re: Using function in a calculation problems

Thanks for responding, I'm checking to see if it works.
Samuel

8. ## Re: Using function in a calculation problems

Originally Posted by GGandhi
i see you are in mobile & Browser forum, where is this calculation being done
in an ux or an updatable grid? if so, then you may want to consider writing the code in javascript. which will not require a callback to the server.
I add the calculation in a UX field calculation. But I'm really confuse with the mod() function(don't figure out how to place the data).

9. ## Re: Using function in a calculation problems

IF(ISODD(UnderBust),UnderBust+5",UnderBust+4")

would become

IF(mod(int(UnderBust),2)>0,UnderBust+5",UnderBust+4")

10. ## Re: Using function in a calculation problems

Originally Posted by Stan Mathews
IF(ISODD(UnderBust),UnderBust+5",UnderBust+4")

would become

IF(mod(int(UnderBust),2)>0,UnderBust+5",UnderBust+4")

I'd try your suggestion, an error massage "mod" is undefined .

11. ## Re: Using function in a calculation problems

Then you're operating in an environment where xbasic functions aren't allowed. See post #6 about using javascript.

Actually the earlier answer was not entirely correct.

The expression should not have the " inch representation.

IF(mod(int(UnderBust),2)>0,UnderBust+5,UnderBust+4)

12. ## Re: Using function in a calculation problems

Originally Posted by Stan Mathews
Then you're operating in an environment where xbasic functions aren't allowed. See post #6 about using javascript.

Actually the earlier answer was not entirely correct.

The expression should not have the " inch representation.

IF(mod(int(UnderBust),2)>0,UnderBust+5,UnderBust+4)

I make the correction, and stillErrorMsg.PNG

13. ## Re: Using function in a calculation problems

mod is an alpha function, highly doubt that it is available at the browser level.
you should be doing this via javascript, otherwise you should do an ajaxcallback to the server where the function is available at the WAS.
additionally you need two step process these two should be able to be combined in one javascript call and the size is captured at the end.
again you did not answer my original question in post number 6. is this on an ux or updatable grid?

14. ## Re: Using function in a calculation problems

I add the calculation in a UX field calculation. #8

15. ## Re: Using function in a calculation problems

okay this is what you do
say if you have three fields
bust a number, under_breast a number and cup a character fields, then in the under_breast onChange event define an inline javascript and copy the following.
Code:
```var underBreast = {dialog.object}.getValue('UNDER_BREAST’);
var onBust = {dialog.object}.getValue(‘BUST’);
var newUnderBreast = null;
if (underBreast%2 == 0) {
newUnderBreast = parseInt(underBreast) + 4;
} else {
newUnderBreast = parseInt(underBreast) + 5;
}
var cupSize = null;
if (onBust - newUnderBreast == 1) {
cupSize = 'A';
}
else if (onBust - newUnderBreast == 2) {
cupSize = 'B';
}
else  {
cupSize = ‘C’;
}

{dialog.object}.setValue(‘CUP’, cupSize);```
and you can change the condition of C as you please.

i am not going to be at my work computer till saturday any fine tuning will take time for me to do.
let us know how it goes.

16. ## Re: Using function in a calculation problems

I'd try and have some error.
Let me make it clear again.
There is a condition by one of the calculation: If UnderBust is an even add 4" if the UnderBust is odd Add 5" if The UnderBust is in between Roundup
Then I get a result number. This number I subtract fro Bust And if the difference is 1" then Cup size is "A" if 2" then "B" if 3" then "C" else "C"
My problem is in the first part of the calculation and it's to add the correct number to UnderBust.
I'll attached some image tha tyou can see the idee.(in mean time I put in my calculation a fix number (UnderBust +10.2cm)(4.5")
calcimage.PNG
CupCalc.PNG

Thanks again
Samuel

17. ## Re: Using function in a calculation problems

Code:
```var underBreast = {dialog.object}.getValue('UNDER_BREASTâ€™);
underBreast = Math.ceil(underBreast);
var onBust = {dialog.object}.getValue(â€˜BUSTâ€™);
var newUnderBreast = null;
if (underBreast%2 == 0) {
newUnderBreast = parseInt(underBreast) + 4;
} else {
newUnderBreast = parseInt(underBreast) + 5;
}
var cupSize = null;
if (onBust - newUnderBreast == 1) {
cupSize = 'A';
}
else if (onBust - newUnderBreast == 2) {
cupSize = 'B';
}
else if (onBust - newUnderbreast == 3) {
cupSize = 'C';
else  {
cupSize = â€˜Dâ€™;
}

{dialog.object}.setValue(â€˜CUPâ€™, cupSize);```
the round up was not mentioned earlier, so i modified the code.
also with this javascript code, you do not need to calculate the cup size it will automatically insert in the cup field.

when i get a chance i will look into how to get any in between number to up-round it.

18. ## Re: Using function in a calculation problems

Originally Posted by esamuel
I'd try and have some error.
Let me make it clear again.
There is a condition by one of the calculation: If UnderBust is an even add 4" if the UnderBust is odd Add 5" if The UnderBust is in between Roundup
Then I get a result number. This number I subtract fro Bust And if the difference is 1" then Cup size is "A" if 2" then "B" if 3" then "C" else "C"
My problem is in the first part of the calculation and it's to add the correct number to UnderBust.
I'll attached some image tha tyou can see the idee.(in mean time I put in my calculation a fix number (UnderBust +10.2cm)(4.5")
calcimage.PNG
CupCalc.PNG

Thanks again
Samuel
Hey Samuel,

What I don't quite follow is where your "either or" calculation is coming from for adding to the new underbust value. It sounds like there's more going on in your calculations than you've initially indicated and the screenshot really doesn't provide extra information as to what maths are involved. Though, maybe that's because the discussion is in inches but your images are showing cm.

I put together a component that tries to do what you're describing. It uses Calculated Field Expressions to calculate the new underbust value as well as the cup size. Is this what you were looking for?

EDIT: there was an error in my calculations if "cm" was selected from the dropdown. This zip contains the fixed version:
underbust.zip

19. ## Re: Using function in a calculation problems

OK, That work.
Did I say Genus?
Now I have to figure out how and were to put it in my app.

Thanks

20. ## Re: Using function in a calculation problems

Hi, Sarah,
I implemented the code where it's belong, and it works.
There is some changes that you can help me make:
1) I need the default to be "cm"(I made changes in Unit control, but I need to change the JavaScript too, but the result was incorrect).
let say I create client with data in "in" and like to see measurement in "cm" and going back to "in , the data was incrementing each time I made this changes.
recordof .PNGrecordof1 .PNGrecordof2 .PNG
I need to fix this incrementation.
2) In the Calculation part You can see image , that when in "cm" the adding sum should be 10.2mm or 12.7mm , but it add 4" or 5" instead,( When the data is in "in" it's all correct.)

21. ## Re: Using function in a calculation problems

Originally Posted by esamuel
Hi, Sarah,
I implemented the code where it's belong, and it works.
There is some changes that you can help me make:
1) I need the default to be "cm"(I made changes in Unit control, but I need to change the JavaScript too, but the result was incorrect).
let say I create client with data in "in" and like to see measurement in "cm" and going back to "in , the data was incrementing each time I made this changes.
recordof .PNGrecordof1 .PNGrecordof2 .PNG
I need to fix this incrementation.
2) In the Calculation part You can see image , that when in "cm" the adding sum should be 10.2mm or 12.7mm , but it add 4" or 5" instead,( When the data is in "in" it's all correct.)
Ah.

What you need to do is convert the data coming in, which is in cm, to inches:
Code:
```var cmInAnInch = 2.54;
underbust = Math.ceil(underbust/cmInAnInch);```
Do all the calculations in inches, then at the end, convert it back to cm:
Code:
`return newUnderbust * cmInAnInch;`
In the case of the cup size calculation, you don't need to convert back to cm since you're returning a string. But for the new underbust, you would need to convert back if you're displaying it.

If you wanted to do all the math in cm, then you can't use the floor or ceiling functions for rounding. Nor can you use the modulo operator (%) based on your requirement that lengths are rounded to the nearest *inch*. The math operators would round your numbers to the nearest cm.

If you want the math that adds the 4" or 5" inches to work with the original cm data, you can store your inches calculations in separate variables and add the cm values to the original data that was given to the function. Also, I'm making the assumption that you meant 10.2cm and not 10.2mm (10.2mm would be 1.2cm and way smaller than 4"). If this assumption is false, then 10.2 and 12.7 need to be changed to 1.02 and 1.27 below:
Code:
```function calcNewUnderbustAlt (underbust) {
var cmInAnInch = 2.54;
var underbustIn = Math.ceil(underbust/cmInAnInch);

var newUnderbust = 0;

// FYI: this does not round the cm value UP to the nearest inch before
if (underbustIn == 0) {
newUnderbust = 0;
} else if (underbustIn % 2 == 0) {
newUnderbust = underbust + 10.2;
} else {
newUnderbust = underbust + 12.7;
}

return newUnderbust;
}```
I updated the component I posted earlier to do the conversion to inches before mathing if the data was cm values after recognizing that Math.ceil() on a cm value didn't round to the nearest *inch*.

22. ## Re: Using function in a calculation problems

dam sarah, nice work!

23. ## Re: Using function in a calculation problems

Code:
```var measurement = {dialog.Object}.getValue('MEASUREMENT');
var bust = {dialog.Object}.getValue('BUST');
var underBust = {dialog.Object}.getValue('UNDER_BUST');
if (measurement == "centimeters") {
bust = bust/2.54;
underBust = underBust/2.54;
}
bust = Math.ceil(bust);
underBust = Math.ceil(underBust);
var newUnderBust = null;
if (underBust%2 == 0) {
newUnderBust = parseInt(underBust) + 4;
} else {
newUnderBust = parseInt(underBust) + 5;
}
var cupSize = null;
switch(bust - newUnderBust) {
case 1:
cupSize = 'A';
break;
case 2:
cupSize = 'B';
break;
case 3:
cupSize = 'c';
break;
case 4:
cupSize = 'D';
break;
default:
cupSize = 'Z';
}
{dialog.Object}.setValue('CUP_SIZE',cupSize);```
here is the follow up on the code in post #17,
it depends on 3 fields: measurement ( can be inches or centimeters on a dropdown control), bust, underbust and sets the value of the cupsize based on the information provided.

24. ## Re: Using function in a calculation problems

take a look at this screen implementing the code, also I think you have an error in your image
if you have bust 91 cm, under_bust at 74cm then the cup size should be B according to calculation I did.
http://screencast.com/t/txQXlkbpT

25. ## Re: Using function in a calculation problems

Originally Posted by GGandhi
take a look at this screen implementing the code, also I think you have an error in your image
if you have bust 91 cm, under_bust at 74cm then the cup size should be B according to calculation I did.
http://screencast.com/t/txQXlkbpT

Thank you for a very nice explanation.
I'll put it to work and inform how it's going

Samuel

26. ## Re: Using function in a calculation problems

Originally Posted by GGandhi
take a look at this screen implementing the code, also I think you have an error in your image
if you have bust 91 cm, under_bust at 74cm then the cup size should be B according to calculation I did.
http://screencast.com/t/txQXlkbpT

Me again,
Can you please send me a zip file of this so I can see all details more clearly?!!

27. ## Re: Using function in a calculation problems

sorry sam I did not save the component I did not think you will need that.
the important aspect is the code, that I gave you. but if that is not enough, you sent me an email, so you have access to me. please send all the measurements and desired results. this weekend I will put together a dialog and zip it to you ( this will be the first time I am sending a component, hopefully I will do right). remember again this will be in version 11. no panel cards etc.,

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•