NumberValue (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
No edit summary
Line 1: Line 1:
{{Template:Json:NumberValue subtitle}}
{{Template:Json:NumberValue subtitle}}
 
This function returns the number value of a Json object of the number type.
This page is [[under construction]].
==Syntax==
==Syntax==
{{Template:Json:NumberValue syntax}}
{{Template:Json:NumberValue syntax}}
Line 8: Line 7:
<tr><th>%number</th><td>number</td></tr>
<tr><th>%number</th><td>number</td></tr>
<tr><th>Json</th>
<tr><th>Json</th>
<td>Json object, which may be <var>Null</var></td></tr>
<td>Json object, which must have an underlying number type. If the Json object is doesn't have the number type, an InvalidJsonType exception is thrown.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<li>If the Json object has a string value that can be converted to a number, the Number method will still throw an InvalidJsonType exception. If a Json string value is known to be convertible to a number, extract it with the [[StringValue (Json function)|StringValue]] function and then assign it to a number (probably Float) variable.</li>
<li>The type of a Json object can be determined with the [[Type (Json function)|Type function]].</li>
</ul>
==Examples==
==Examples==
The following example extracts the number values from items in an array and totals them:
<p class="code">b
%json  is object json
%i      is float
%total  is float
%json = array(1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012)
for %i from 1 to %json:count
  %total = %total + %json(%i):numberValue
end for
printText {~=%total}
end
</p>
This prints:
<p class="code">%total=290512
</p>
==See also==
==See also==
{{Template:Json:NumberValue footer}}
{{Template:Json:NumberValue footer}}

Revision as of 19:19, 18 February 2015

Get number value of JSON object (Json class)

[Introduced in Model 204 7.6]

This function returns the number value of a Json object of the number type.

Syntax

%number = json:NumberValue Throws InvalidJsonType

Syntax terms

%numbernumber
Json Json object, which must have an underlying number type. If the Json object is doesn't have the number type, an InvalidJsonType exception is thrown.

Usage notes

  • If the Json object has a string value that can be converted to a number, the Number method will still throw an InvalidJsonType exception. If a Json string value is known to be convertible to a number, extract it with the StringValue function and then assign it to a number (probably Float) variable.
  • The type of a Json object can be determined with the Type function.

Examples

The following example extracts the number values from items in an array and totals them:

b %json is object json %i is float %total is float %json = array(1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012) for %i from 1 to %json:count %total = %total + %json(%i):numberValue end for printText {~=%total} end

This prints:

%total=290512

See also