Macro Express Forums

Verify Decimal in a value

Recommended Posts

Hi,

Is there a way to check decimal(.) in a value ?

I'm capturing some values from an application and doing some calculation on it. After that i need to check if the final value consist decimal or not.

I tried to convert the integer/decimal to string as well but it doesn't retains the value after decimal.

Rocki

Share on other sites

Could you give an example of what you are trying to do?

At first I thought that you want to test whether a string variable contains a decimal point, but now I am not sure.

Share on other sites

Here's the script.

Variable Set String %T[90]%: Prompt
Variable Set String %T[91]%: Prompt
Variable Modify String %T[90]%: Convert to Decimal (%D[1]%)
Variable Modify String %T[91]%: Convert to Decimal (%D[2]%)
Variable Modify Decimal: %D[3]% = %D[1]% / %D[2]%
Variable Modify Decimal: Round %D[3]% to 2 decimal places

If Variable %D[3]% Contains "."
Text Box Display:
Else
Text Box Display:
End If

I want the code to give a message "Evenly Divided" or "Result contaions a decimal". For example : 10 / 2 = 5 which would result first message and 10 / 3 = 3.34 which would result in second message.

I've tried with direct values in Integer and Decimal as well(without conversion) but it didn't work as well.

I need to follow a different set of steps for both conditions mentioned above. Hope this explanation helps.

Share on other sites

You cannot check for a "." in a decimal variable. You could check for "." in a string variable. This is not valid:

```If Variable %D[3]% Contains "."
```
I think this works the same as

```If Variable %D[3]% Contains 0.0
```

Use the "Variable Modify Decimal Remove Integer Portion" command. Like this:

```Variable Set to ASCII Char 9 to %TAB% // Initialize TAB

Variable Set String %T[90]%: Prompt // Get numerator
Variable Set String %T[91]%: Prompt // Get denominator
Variable Modify String %T[90]%: Convert to Decimal (%D[1]%) // Convert numerator to a decimal variable
Variable Modify String %T[91]%: Convert to Decimal (%D[2]%) // Convert denominator to a decimal variable
Variable Modify Decimal: %D[3]% = %D[1]% / %D[2]%
Variable Modify Decimal: Round %D[3]% to 2 decimal places

Variable Modify Decimal %D[3]%: Remove Integer Portion (%D[4]%)

// Test to see if evenly divided
If Variable %D[4]% Equals "0"
Text Box Display: Integer value
Else
Text Box Display: Decimal value
End If

```
Copy/paste this:

```<VARIABLE SET TO ASCII CHAR Value="9" Destination="%TAB%" _COMMENT="Initialize TAB"/>
<COMMENT/>
<VARIABLE SET STRING Option="\x01" Destination="%T[90]%" Prompt="Enter numerator" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _COMMENT="Get numerator"/>
<VARIABLE SET STRING Option="\x01" Destination="%T[91]%" Prompt="Enter denominator" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _COMMENT="Get denominator"/>
<VARIABLE MODIFY STRING Option="\x05" Destination="%T[90]%" Variable="%D[1]%" _COMMENT="Convert numerator to a decimal variable\r\n"/>
<VARIABLE MODIFY STRING Option="\x05" Destination="%T[91]%" Variable="%D[2]%" _COMMENT="Convert denominator to a decimal variable\r\n"/>
<VARIABLE MODIFY DECIMAL Option="\x03" Destination="%D[3]%" Value1="%D[1]%" Value2="%D[2]%"/>
<VARIABLE MODIFY DECIMAL Option="\x04" Destination="%D[3]%" Places="2"/>
<COMMENT/>
<VARIABLE MODIFY DECIMAL Option="\x07" Destination="%D[3]%" Variable="%D[4]%"/>
<COMMENT/>
<COMMENT Value="Test to see if evenly divided"/>
<IF VARIABLE Variable="%D[4]%" Condition="\x00" Value="0" IgnoreCase="FALSE"/>
<TEXT BOX DISPLAY Title="Integer value" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 This is an integer value because the fractional part is 0\r\n\\par \r\n\\par Numerator:%TAB%%D[1]%\r\n\\par Denominator:%TAB%%D[2]%\r\n\\par Result:%TAB%%TAB%%D[3]%\r\n\\par Fractional:%TAB%%D[4]%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="335" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<ELSE/>
<TEXT BOX DISPLAY Title="Decimal value" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 This is a decimal value because the fractional part is not 0\r\n\\par \r\n\\par Numerator:%TAB%%D[1]%\r\n\\par Denominator:%TAB%%D[2]%\r\n\\par Result:%TAB%%TAB%%D[3]%\r\n\\par Fractional:%TAB%%D[4]%\\f1 \r\n\\par \\f0 \r\n\\par }\r\n" Left="Center" Top="Center" Width="331" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<END IF/>

```
Share on other sites

Thanks Samrae for explaining so well.

However i'm new to the ASCII value and haven't used this option before. So not able to understand what you did in the below code.

```Variable Set to ASCII Char 9 to %TAB% // Initialize TAB

```

Can you explain a bit more about it. Though i tried to get some info from the help but still not clear on this.

Share on other sites

The only reason I used the Variable Set to ASCII Char command is to allow the variables to line up in the Text Box Display command. It is purely for cosmetic purposes. You do not need it in your macro unless you too want to line up the variable results.

This creates the variable %TAB% and sets it to the ASCII value for a TAB character.

```Variable Set to ASCII Char 9 to %TAB% // Initialize TAB
```
Then, if the Text Box Display command contains this:

```Numerator:%TAB%%D[1]%
Denominator:%TAB%%D[2]%
Result:%TAB%%TAB%%D[3]%
Fractional:%TAB%%D[4]%
```
The results look like this:

```This is a decimal value because the fractional part is not 0

Numerator:	12
Denominator:	7
Result:		1.71
Fractional:	0.71
```
If you do not use TABs, like this:

```Numerator: %D[1]%
Denominator: %D[2]%
Result: %D[3]%
Fractional: %D[4]%
```
The the Text Box Display looks like this:

```This is a decimal value because the fractional part is not 0

Numerator: 12
Denominator: 7
Result: 1.71
Fractional: 0.71
```
You can use spaces but the results may not line up exactly.
Share on other sites

Thanks for explaining it so well Samrae.

I used the logic you mentioned above. However this doesn't solve the issue.

Example :If we had \$ 385.35 with 3 units 385.35 can be divided by 3 evenly ( meaning you can split 385.35 into 3 equal parts. ) equal part being 128.45. However, If we had 385.35 and 4 units 385.35 cannot be divided by 4 evenly ( meaning you cannot split 385.35 into 4 equal parts) you would have 96.34 + 96.34 + 96.34 + 96.33 all lines cannot be the same.

Is there any other way by which we can deal with this issue ?

Share on other sites

• 2 weeks later...

Only just seen this thread. Isn't your last post raising a completely different question to the one that Samrae answered? Although I'm not exactly sure what it is? Can you give more background on the application and purpose? Perhaps we're into the topic of rounding for practical use, e.g. on a price tag (\$96.34) versus precision, e.g. for banking (\$96.3375000)?

--

Share on other sites

It is in Continuance with what i previously asked . I'm in a healthcare project where some charges needs to be splitted on the basis of the multiple units billed for that day. I need to split the total charges field with the no of units billed for that particular service. This example must give you an idea about what needs to be achieved.

Example :If we had \$ 385.35 with 3 units 385.35 can be divided by 3 evenly ( meaning you can split 385.35 into 3 equal parts. ) equal part being 128.45. However, If we had 385.35 and 4 units 385.35 cannot be divided by 4 evenly ( meaning you cannot split 385.35 into 4 equal parts) you would have 96.34 + 96.34 + 96.34 + 96.33 all lines cannot be the same.

Share on other sites

Well, maybe someone else can follow you, but I'm afraid I don't. You haven't added anything new in that example. And it's not true that "...385.35 cannot be divided by 4 evenly ( meaning you cannot split 385.35 into 4 equal parts)". You can. The result, as I pointed out in my last post, is 96.3375000 to six decimal places. It's just a matter of specifying the precision required. Which, as I said, will depend on the intended practical purpose. (Which you still haven't described). I gave two broad application examples.

BTW, I'm unclear what you mean by "all lines cannot be the same" ? What 'lines'? Do you mean the prices resulting from this division? If so, presumably you mean "All prices must be exactly the same."

My view remains that this new question seems quite different to your original question: "...i need to check if the final value consist decimal or not." You got a thorough answer to that one.

Anyway, good luck with whatever you're trying to do.

--

Share on other sites

This just becomes a simple math problem. If you want to know if \$385.35 is evenly divisible by 3 (to the nearest cent) then do this:

\$385.35 * 100 = \$38535

\$38535 / 3 = \$12845

Get fractional part (using technique in sample macro above)

Fraction = 0 means evenly divisible

\$385.35 * 100 = \$38535

\$38535 / 4 = \$9633.75

Get fractional part (using technique in sample macro above)

Fraction = .75 means not evenly divisible

Share on other sites

Assuming your requirement is as Kevin described (and that would make sense to me) then here's my modified version of Samrae's macro that hopefully does what you want.

I've left various text messages to make sure it's clear.

Perhaps someone can explain to me why MX calculates the remainder for the 385.35 / 4 example as 0.00249999999999773 and not 0.0025000000000000, or at least something closer to 0.0025?

Copy/paste this, or run the attached macro.

```<VARIABLE SET STRING Option="\x01" Destination="%T[90]%" Prompt="Enter numerator" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _COMMENT="Get numerator"/>
<VARIABLE SET STRING Option="\x01" Destination="%T[91]%" Prompt="Enter denominator" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _COMMENT="Get denominator"/>
<VARIABLE MODIFY STRING Option="\x05" Destination="%T[90]%" Variable="%D[1]%" _COMMENT="Convert numerator to a decimal variable\r\n"/>
<VARIABLE MODIFY STRING Option="\x05" Destination="%T[91]%" Variable="%D[2]%" _COMMENT="Convert denominator to a decimal variable\r\n"/>
<VARIABLE MODIFY DECIMAL Option="\x03" Destination="%D[3]%" Value1="%D[1]%" Value2="%D[2]%" _COMMENT="Divide to get exact result to 16 decimal places"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 D[3]\\f1  = \\f0 %D[3]%\\f1  = Original exact result of division\\f0 \r\n\\par \r\n\\par }\r\n" Left="821" Top="Center" Width="364" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<VARIABLE MODIFY DECIMAL Option="\x03" Destination="%D[4]%" Value1="%D[1]%" Value2="%D[2]%"/>
<VARIABLE MODIFY DECIMAL Option="\x04" Destination="%D[4]%" Places="2" _COMMENT="Get result rounded to two decimal places"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 D[\\f1 4\\f0 ]\\f1  = \\f0 %D[\\f1 4\\f0 ]%\\f1  = Result of division rounded to 2 decimal places\\f0 \r\n\\par \r\n\\par }\r\n" Left="821" Top="Center" Width="363" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<VARIABLE MODIFY DECIMAL Option="\x01" Destination="%dDifference%" Value1="%D[4]%" Value2="%D[3]%" _COMMENT="Subtract to get the difference"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 d\\f1 Difference\\f0  = \\f1 %\\f0 d\\f1 Difference%\\f0  = Difference between rounded and exact result.\\f1 \r\n\\par \r\n\\par \r\n\\par }\r\n" Left="753" Top="Center" Width="514" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<COMMENT Value="Now find the ABSOLUTE value of the difference"/>
<COMMENT/>
<IF VARIABLE Variable="%dDifference%" Condition="\x02" Value="0" IgnoreCase="FALSE" _COMMENT="Test if negative"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%dDifference%" Value1="%dDifference%" Value2="-1"/>
<END IF/>
<COMMENT Value="Test to see if evenly divided"/>
<IF VARIABLE Variable="%dDifference%" Condition="\x02" Value="0.00000000001" IgnoreCase="FALSE"/>
<TEXT BOX DISPLAY Title="Integer value" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 This is an evenly divided result (to the cent) because the difference is zero or extremely close to it.\r\n\\par \r\n\\par Numerator: D[1]%\r\n\\par Denominator: %D[2]%\r\n\\par Exact result: D[3]%\r\n\\par Rounded result to two places: %D[4]%\r\n\\par Difference (absolute): %dDifference]%\\f1 \r\n\\par }\r\n" Left="775" Top="Center" Width="433" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<ELSE/>
<TEXT BOX DISPLAY Title="Decimal value" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 This is \\ul\\b not\\ulnone\\b0  an evenly divided result (to the cent) because the difference is not equal to or very close to 0\r\n\\par \r\n\\par Numerator: %D[1]%\r\n\\par Denominator: %D[2]%\r\n\\par Exact result: %D[3]%\r\n\\par Rounded result to two places: %D[4]%\r\n\\par Difference (absolute): %dDifference%\r\n\\par }\r\n" Left="800" Top="490" Width="381" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<END IF/>

```

--

CheckEvenlyDivided.mex

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.