Difference between revisions of "Numbers and text"

m
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:Analytica User Guide]]
 
[[Category:Analytica User Guide]]
<breadcrumbs>Analytica User Guide > Using Expressions > {{PAGENAME}}</breadcrumbs>
+
<breadcrumbs>Analytica User Guide > Expressions > {{PAGENAME}}</breadcrumbs>
 
+
{{ReleaseBar}}
  
 
'''Converting number to text''': If you apply the [[Text_Concatenation_Operator%3A_%26|&]] operator or [[JoinText]]() to numbers, they convert the numbers to text values, using the number format specified for the variable or function in whose definition they appear. You can use this effect to convert (“coerce”) numbers into text values, for example:
 
'''Converting number to text''': If you apply the [[Text_Concatenation_Operator%3A_%26|&]] operator or [[JoinText]]() to numbers, they convert the numbers to text values, using the number format specified for the variable or function in whose definition they appear. You can use this effect to convert (“coerce”) numbers into text values, for example:
:<code>123456789 & '' &rarr; '123.5M'</code>
+
:<code>123456789 & "" &rarr; '123.5M'</code>
:<code>123456789 & '' &rarr; '$123,456,789.00'</code>
+
:<code>123456789 & "" &rarr; '$123,456,789.00'</code>
 
:<code>'The date is: ' & 38345 &rarr; The date is: Thursday, December 25, 2008'</code>
 
:<code>'The date is: ' & 38345 &rarr; The date is: Thursday, December 25, 2008'</code>
  
<tip title="Tip">The actual result depends on ''Number Format ''setting for the variable or function in whose definition the expression appears. The first example assumes the default ''Suffix ''format. The second assumes ''Fixed Point ''format, with currency and thousands separators checked, and two decimal digits. The third assumes the ''Long Date ''format. Use the '''Number format '''dialog on the '''Result '''menu to set the formats.</tip>
+
<tip title="Tip">The actual result depends on ''Number Format ''setting for the variable or function in whose definition the expression appears. The first example assumes the default ''Suffix ''format. The second assumes ''Fixed Point ''format, with currency and thousands separators checked, and two decimal digits. The third assumes the ''Long Date ''format. Use the [[Number format]] dialog on the [[Result menu]] to set the formats.</tip>
 +
 
 +
You can also control the format used when converting numbers to text with the [[Text functions|NumberToText]] function, in which case the result does is not a function of the current number format setting.
 +
:<code>[[NumberToText]](123456789, 'Exponential') &rarr; '1.235e+008'</code>
 +
 
 +
{{Release|6.0||Another way to control the format is by using [[Formatted Text Literals]] with a format specifier.
 +
:<code>f"{123456789:F2,0}" &rarr; 1.235e+008</code> &rarr; '123,456,789.00'}}
  
You can also control the format used when converting numbers to text with the [[Text functions|NumberToText]] function.
+
'''Converting text to number''': Use the [[ParseNumber]] function to convert a text representation of a number into an actual number. For example,
 +
:<code>[[ParseNumber]]('12350') &rarr; 12.35K</code>
  
'''Converting text to number''': You can use the [[Evaluate]]() function to convert a text representation of a number into an actual number, for example:
+
[[ParseNumber]]() can convert any number format that Analytica can handle in an expression— and no others. Thus, it can handle decimals, exponent format,  <code>True</code> or <code>False</code>, a <code>$</code> at the start of a number (which it ignores), and letter suffixes, like <code>K</code> and <code>M</code>, hex and binary. To parse dates and times, use the [[ParseDate]] function.
:<code>Evaluate('12350') &rarr; 12.35K</code>
 
  
Evaluate() can convert any number format that Analytica can handle in an expression— and no others. Thus, it can handle decimals, exponent format, dates, <code>True</code> or <code>False</code>, a <code>$</code> at the start of a number (which it ignores), and letter suffixes, like <code>K</code> and <code>M</code>.
+
A second parameter to [[ParseNumber]] specifies the result when the text cannot be parsed as a number. To retain the unparsed original value in that case, use <code>[[ParseNumber]]( x, x )</code>.
  
 
An alternative method, for converting text to a number is to use the <code>Coerce Number</code> qualifier on a user-defined function. For example, you could define a user-defined function such as:
 
An alternative method, for converting text to a number is to use the <code>Coerce Number</code> qualifier on a user-defined function. For example, you could define a user-defined function such as:
Line 21: Line 27:
  
 
==See Also==
 
==See Also==
 +
* [[Converting Numbers to Text]]
 
* [[NumberToText]]
 
* [[NumberToText]]
 
* [[Numbers]]
 
* [[Numbers]]
 +
* [[IsNumber]]
 +
* [[IsText]]
 
* [[Math functions]]
 
* [[Math functions]]
 
* [[Text functions]]
 
* [[Text functions]]
 +
* [[Read and write text files]]
 +
* [[Multiple formats in one table]]
  
  
 
<footer>Math functions / {{PAGENAME}} / INF, NAN, and NULL - Exception values</footer>
 
<footer>Math functions / {{PAGENAME}} / INF, NAN, and NULL - Exception values</footer>

Latest revision as of 00:10, 25 March 2021



Release:

4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5


Converting number to text: If you apply the & operator or JoinText() to numbers, they convert the numbers to text values, using the number format specified for the variable or function in whose definition they appear. You can use this effect to convert (“coerce”) numbers into text values, for example:

123456789 & "" → '123.5M'
123456789 & "" → '$123,456,789.00'
'The date is: ' & 38345 → The date is: Thursday, December 25, 2008'
Tip
The actual result depends on Number Format setting for the variable or function in whose definition the expression appears. The first example assumes the default Suffix format. The second assumes Fixed Point format, with currency and thousands separators checked, and two decimal digits. The third assumes the Long Date format. Use the Number format dialog on the Result menu to set the formats.

You can also control the format used when converting numbers to text with the NumberToText function, in which case the result does is not a function of the current number format setting.

NumberToText(123456789, 'Exponential') → '1.235e+008'

Another way to control the format is by using Formatted Text Literals with a format specifier.

f"{123456789:F2,0}" → 1.235e+008 → '123,456,789.00'

Converting text to number: Use the ParseNumber function to convert a text representation of a number into an actual number. For example,

ParseNumber('12350') → 12.35K

ParseNumber() can convert any number format that Analytica can handle in an expression— and no others. Thus, it can handle decimals, exponent format, True or False, a $ at the start of a number (which it ignores), and letter suffixes, like K and M, hex and binary. To parse dates and times, use the ParseDate function.

A second parameter to ParseNumber specifies the result when the text cannot be parsed as a number. To retain the unparsed original value in that case, use ParseNumber( x, x ).

An alternative method, for converting text to a number is to use the Coerce Number qualifier on a user-defined function. For example, you could define a user-defined function such as:

ParseNum(X: Coerce Number) := X

See Also


Comments


You are not allowed to post comments.