Difference between revisions of "XML: Macros - Extracting Digits"

From FSDeveloper Wiki
Jump to: navigation, search
Line 13: Line 13:
 
This XML Macro is very handy for the purpose, as it is "universal" and does not care what the number is...
 
This XML Macro is very handy for the purpose, as it is "universal" and does not care what the number is...
  
<Macro Name="ExtDigit">  
+
<Macro Name="ExtDigit">  
@1 sp0  
+
@1 sp0  
@3 0 >   
+
@3 0 >   
if{ l0 10 @3 pow * sp0 }  
+
if{ l0 10 @3 pow * sp0 }  
l0 int 10 @2 pow % 10 @2 1 - 0 max pow / int   
+
l0 int 10 @2 pow % 10 @2 1 - 0 max pow / int   
</Macro>
+
</Macro>
  
 
   
 
   

Revision as of 12:12, 5 October 2009

Extract Digits Macro

Quite frequently it is necessary to extract the nth digit of a number for any number of reasons. This XML Macro is very handy for the purpose, as it is "universal" and does not care what the number is...

<Macro Name="ExtDigit"> 
@1 sp0 
@3 0 >  
if{ l0 10 @3 pow * sp0 } 
l0 int 10 @2 pow % 10 @2 1 - 0 max pow / int  
</Macro>


Where: 
(ALGVar) is the A,L or G var from where to extract the digit 
nDigit is the position of the digit to extract, starting from the right. 
nDecimals is the number of decimals to be included in the calcs. 

For instance
for a value of 123.456789999999 

@ExtDigit(lVar,1,0) means I want to extract the right most, discarding the decimals. It returns 3 
@ExtDigit(lVar,2,0) returns 2 ; @ExtDigit(lVar,3,0) returns 1 and 
@ExtDigit(lVar,4,0) and up returns 0 

using decimals: 
@ExtDigit(lVar,1,2) returns 5  
@ExtDigit(lVar,2,2) returns 4  
@ExtDigit(lVar,3,2) returns 3  
@ExtDigit(lVar,4,2) returns 2