Difference between revisions of "SsParseNum"

From SmartWiki
Jump to: navigation, search
(Created page with 'The ''ssParseNum'' syntax is used in JavaScript expressions ==See Also== * SsFormatCurrency Category:JavaScriptCategory:Currency')
 
(When Using "Show Currency Format in Edit Mode")
 
(20 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The ''ssParseNum'' syntax is used in JavaScript expressions
+
{{JavaScript Syntax}}
 +
__TOC__
 +
==Overview==
 +
To convert a number from a formatted string (containing symbols like "$" or "%") to a number using [[JavaScript]] you can use a function called ''ssParseNum''.
 +
 
 +
==Syntax==
 +
:<font size="3">'''ssParseNum( ''value'', ''emptyok'' );''' </font>
 +
 
 +
Where: <br / >
 +
 
 +
{| border="1"
 +
!Parameter
 +
!Description
 +
|-
 +
|<b><em>value </em></b>
 +
|Required. Is the value to be converted to a number (i.e: $5,000.00 and 5,000% will be converted to 5000)<br />
 +
|-
 +
|<b><em>emptyok </em></b>
 +
|Optional. Is a true/false value
 +
:*if ''emptyok'' is true - an empty string will be returned by ssParseNum if the value is ''empty''
 +
:*if ''emptyok'' is false - a "0" will be returned by ssParseNum if the value is ''empty''
 +
|}
 +
===Examples===
 +
<pre>
 +
ssParseNum($5,000.50);
 +
</pre>
 +
 
 +
Will return: 5000.5
 +
 
 +
----
 +
 
 +
If you wish to display a ratio of two fields that are set to have currency formatting, you should convert the values to numbers using ''ssParseNum'':
 +
 
 +
<pre>
 +
ssParseNum([field1])/ssParseNum([field2])
 +
</pre>
 +
 
 +
==Requirements==
 +
This function is part of the <b>sstools.js</b> library and needs to be added to [[Browser Script]] custom fields or on template pages, sign-up pages etc. using the following syntax:
 +
<SCRIPT type="text/javascript" language="javascript" src="/sstools.js"></SCRIPT>
 +
 
 +
==When Using "Show Currency Format in Edit Mode"==
 +
When using the "Show Currency Format in Edit Mode" setting for custom fields, the following syntax must be used:
 +
 
 +
: <span style="font-size: medium;">'''ssParseNumFromCurrency(value,getLocalestrByCurrency(code),1,emptyok);''' ''(getLocalestrByCurrency will get the "default" locale of the currency code, e.g. CAD will result in en-CA, NOT fr-CA)'''''<br /><br />OR<br /><br />ssParseNumFromCurrency(value,locale,1,emptyok); '''''(if you know the locale of the formatted currency, e.g. da-DK or fr-FR or fr-CA etc)'''<br />'''''</span>
 +
Where:
 +
 
 +
{| border="1"
 +
|-
 +
!|Parameter
 +
!|Description
 +
|-
 +
||'''''value '''''
 +
||Required. Is the value to be converted to a number (i.e: $5,000.00 and 5,000 will be converted to 5000)
 +
|-
 +
||'''''emptyok '''''
 +
||Optional. Is a true/false value
 +
:* if ''emptyok'' is true - an empty string will be returned by if the value is ''empty''
 +
:* if ''emptyok'' is false - a "0" will be returned by if the value is ''empty''
 +
 
 +
|-
 +
||'''''code'''''
 +
||Required. The three-letter currency code defined by [http://en.wikipedia.org/wiki/ISO_4217 ISO 4217]. Example: ''JPY'' for Japanese Yen.
 +
|}
 +
 
 +
Example:
 +
 
 +
: <span style="font-size: medium;">ssParseNumFromCurrency([YER Total revenue],getLocalestrByCurrency('CAD'),1,true) - ssParseNumFromCurrency([YER Total expenses],getLocalestrByCurrency('CAD'),1,true)<br /></span>
 +
: <span style="font-size: medium;">or</span>
 +
: <span style="font-size: medium;">ssParseNumFromCurrency([YER Total revenue],'fr-CA',1,true) - ssParseNumFromCurrency([YER Total expenses],'fr-CA',1,true)  <br /><br /></span>
  
 
==See Also==
 
==See Also==
 
+
* [[Custom Field Type: Special – Calculated Value]]
 
* [[SsFormatCurrency]]
 
* [[SsFormatCurrency]]
  
 
[[Category:JavaScript]][[Category:Currency]]
 
[[Category:JavaScript]][[Category:Currency]]

Latest revision as of 13:46, 19 May 2021

This feature uses JavaScript syntax
Text code javascript.gif

Overview

To convert a number from a formatted string (containing symbols like "$" or "%") to a number using JavaScript you can use a function called ssParseNum.

Syntax

ssParseNum( value, emptyok );

Where:

Parameter Description
value Required. Is the value to be converted to a number (i.e: $5,000.00 and 5,000% will be converted to 5000)
emptyok Optional. Is a true/false value
  • if emptyok is true - an empty string will be returned by ssParseNum if the value is empty
  • if emptyok is false - a "0" will be returned by ssParseNum if the value is empty

Examples

ssParseNum($5,000.50);

Will return: 5000.5


If you wish to display a ratio of two fields that are set to have currency formatting, you should convert the values to numbers using ssParseNum:

ssParseNum([field1])/ssParseNum([field2])

Requirements

This function is part of the sstools.js library and needs to be added to Browser Script custom fields or on template pages, sign-up pages etc. using the following syntax:

<SCRIPT type="text/javascript" language="javascript" src="/sstools.js"></SCRIPT>

When Using "Show Currency Format in Edit Mode"

When using the "Show Currency Format in Edit Mode" setting for custom fields, the following syntax must be used:

ssParseNumFromCurrency(value,getLocalestrByCurrency(code),1,emptyok); (getLocalestrByCurrency will get the "default" locale of the currency code, e.g. CAD will result in en-CA, NOT fr-CA)

OR

ssParseNumFromCurrency(value,locale,1,emptyok);
(if you know the locale of the formatted currency, e.g. da-DK or fr-FR or fr-CA etc)

Where:

Parameter Description
value Required. Is the value to be converted to a number (i.e: $5,000.00 and 5,000 will be converted to 5000)
emptyok Optional. Is a true/false value
  • if emptyok is true - an empty string will be returned by if the value is empty
  • if emptyok is false - a "0" will be returned by if the value is empty
code Required. The three-letter currency code defined by ISO 4217. Example: JPY for Japanese Yen.

Example:

ssParseNumFromCurrency([YER Total revenue],getLocalestrByCurrency('CAD'),1,true) - ssParseNumFromCurrency([YER Total expenses],getLocalestrByCurrency('CAD'),1,true)
or
ssParseNumFromCurrency([YER Total revenue],'fr-CA',1,true) - ssParseNumFromCurrency([YER Total expenses],'fr-CA',1,true)  

See Also