Difference between revisions of "ParseNumber"

m
 
(9 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
  
''New to [[What's new in Analytica 4.2?|Analytica 4.2]] as a built-in function'', but an equivalent library function, [[ParseNum]] can be used in Analytica 4.1.
+
==ParseNumber(x'', badVal, decimal'') ==
 +
It converts text «x» to a numeric value, if it has a number format (including dates or time) that is valid in Analytica. If not, e.g. <code>ParseNumber("hello")</code>, it returns [[Null]], or «badVal» if specified. If «x» is already a number, it just returns the value «x».  
  
= [[ParseNumber]](x) =
+
If «x» uses a comma for the decimal point, such as <code>"3.14"</code>, specify the «decimal» parameter to be <code>','</code>.  («decimal» requires [[Analytica 5.0]])
 
 
Parses a string to a numeric value. 
 
 
 
= Library =
 
  
 +
== Library ==
 
Text Functions
 
Text Functions
  
= Example =
+
== Example ==
 
 
[[ParseNumber]]("3.14") &rarr; 3.14
 
 
 
= Notes =
 
 
 
If «x» is already numeric, «x» is returned.
 
  
An array can be passed, in which each cell of the array is parsed.
+
:<code>[[ParseNumber]]('3.14') &rarr; 3.14</code>
 +
:<code>[[ParseNumber]]( x, x )</code>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ ''Keeps cells that don't parse as text'' }
 +
:<code>[[ParseNumber]]('3,14', decimal:',') &rarr; 3.14</code>
  
When x does not parse as a numeric value, such as ParseNum("hello"), returns [[«null»]]
+
== Notes ==
 +
If «x» is an array, it will try to parse each element of the array -- it fully supports[[ Intelligent Arrays]], like most Analytica functions.
  
ParseNum is often useful when reading data from flat files, using [[ReadCsvFile]] or [[ReadTextFile]].
+
[[ParseNumber]] is often useful when reading data from flat files, using [[ReadCsvFile]] or [[ReadTextFile]]. [[ParseCSV] tries to parse numbers automatically (by default), so you don't need to use [[ParseNumber]].
  
Coercion to numeric can be accomplished in Analytica in other ways.  For example, you can declare a function parameter with the [[Function Parameter Qualifiers|qualifiers]] "coerce numeric". Then a string value passed to that parameter is parsed.  The calling variable needs to have a non-date number format, since the number format of the calling object determines how the value is interpreted.
+
You can coerce a text to be numeric in other ways.  For example, you can declare a function parameter with the [[Function Parameter Qualifiers|qualifiers]] "Coerce Numeric". If the actual parameter is text, it will try to convert it to a number when it calls the function.  The Number format of the variable that contains the call to the function determines how the value is interpreted. Usually, it should not be a date number format.
  
= See Also =
+
==History==
 +
* [[ParseNumber]] was introdued to [[What's new in Analytica 4.2?|Analytica 4.2]] as a built-in function.
 +
* The «decimal» parameter was introduced in [[Analytica 5.0]]
  
 +
== See Also ==
 +
<div style="column-count:2;-moz-column-count:2;-webkit-column-count:2">
 +
* [[ParseNum]]
 +
* [[NumberToText]]
 +
* [[Number format]]
 +
* [[NumberFormat]] attribute
 +
* [[Binary and hexadecimal integer formats]]
 +
* [[Base Conversion Function Library]]
 
* [[ParseDate]]
 
* [[ParseDate]]
* [[ReadTextFile]], [[ReadCsvFile]]
+
* [[ReadTextFile]]
 +
* [[ReadCsvFile]]
 +
* [[Text functions]]
 
* [[Function Parameter Qualifiers]] (especially the '''coerce''' qualifier)
 
* [[Function Parameter Qualifiers]] (especially the '''coerce''' qualifier)
 +
</div>

Latest revision as of 20:10, 14 April 2018


ParseNumber(x, badVal, decimal)

It converts text «x» to a numeric value, if it has a number format (including dates or time) that is valid in Analytica. If not, e.g. ParseNumber("hello"), it returns Null, or «badVal» if specified. If «x» is already a number, it just returns the value «x».

If «x» uses a comma for the decimal point, such as "3.14", specify the «decimal» parameter to be ','. («decimal» requires Analytica 5.0)

Library

Text Functions

Example

ParseNumber('3.14') → 3.14
ParseNumber( x, x )      { Keeps cells that don't parse as text }
ParseNumber('3,14', decimal:',') → 3.14

Notes

If «x» is an array, it will try to parse each element of the array -- it fully supportsIntelligent Arrays, like most Analytica functions.

ParseNumber is often useful when reading data from flat files, using ReadCsvFile or ReadTextFile. [[ParseCSV] tries to parse numbers automatically (by default), so you don't need to use ParseNumber.

You can coerce a text to be numeric in other ways. For example, you can declare a function parameter with the qualifiers "Coerce Numeric". If the actual parameter is text, it will try to convert it to a number when it calls the function. The Number format of the variable that contains the call to the function determines how the value is interpreted. Usually, it should not be a date number format.

History

See Also

Comments


You are not allowed to post comments.