Float.ToString(Single) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a string representation of the float
argument.
[Android.Runtime.Register("toString", "(F)Ljava/lang/String;", "")]
public static string ToString(float f);
[<Android.Runtime.Register("toString", "(F)Ljava/lang/String;", "")>]
static member ToString : single -> string
Parameters
- f
- Single
the float
to be converted.
Returns
a string representation of the argument.
- Attributes
Remarks
Returns a string representation of the float
argument. All characters mentioned below are ASCII characters. <ul> <li>If the argument is NaN, the result is the string "NaN
". <li>Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If the sign is negative, the first character of the result is '-
' ('\u005Cu002D'
); if the sign is positive, no sign character appears in the result. As for the magnitude m: <ul> <li>If m is infinity, it is represented by the characters "Infinity"
; thus, positive infinity produces the result "Infinity"
and negative infinity produces the result "-Infinity"
. <li>If m is zero, it is represented by the characters "0.0"
; thus, negative zero produces the result "-0.0"
and positive zero produces the result "0.0"
.
<li> Otherwise m is positive and finite. It is converted to a string in two stages: <ul> <li> <em>Selection of a decimal</em>: A well-defined decimal d<sub>m</sub> is selected to represent m. This decimal is (almost always) the <em>shortest</em> one that rounds to m according to the round to nearest rounding policy of IEEE 754 floating-point arithmetic. <li> <em>Formatting as a string</em>: The decimal d<sub>m</sub> is formatted as a string, either in plain or in computerized scientific notation, depending on its value. </ul> </ul> </ul>
A <em>decimal</em> is a number of the form s×10<sup>i</sup> for some (unique) integers s > 0 and i such that s is not a multiple of 10. These integers are the <em>significand</em> and the <em>exponent</em>, respectively, of the decimal. The <em>length</em> of the decimal is the (unique) positive integer n meeting 10<sup>n-1</sup> ≤ s < 10<sup>n</sup>.
The decimal d<sub>m</sub> for a finite positive m is defined as follows: <ul> <li>Let R be the set of all decimals that round to m according to the usual <em>round to nearest</em> rounding policy of IEEE 754 floating-point arithmetic. <li>Let p be the minimal length over all decimals in R. <li>When p ≥ 2, let T be the set of all decimals in R with length p. Otherwise, let T be the set of all decimals in R with length 1 or 2. <li>Define d<sub>m</sub> as the decimal in T that is closest to m. Or if there are two such decimals in T, select the one with the even significand. </ul>
The (uniquely) selected decimal d<sub>m</sub> is then formatted. Let s, i and n be the significand, exponent and length of d<sub>m</sub>, respectively. Further, let e = n + i - 1 and let s<sub>1</sub>…s<sub>n</sub> be the usual decimal expansion of s. Note that s<sub>1</sub> ≠ 0 and s<sub>n</sub> ≠ 0. Below, the decimal point '.'
is '\u005Cu002E'
and the exponent indicator 'E'
is '\u005Cu0045'
. <ul> <li>Case -3 ≤ e < 0: d<sub>m</sub> is formatted as 0.0
…0
<!-- -->s<sub>1</sub>…s<sub>n</sub>, where there are exactly -(n + i) zeroes between the decimal point and s<sub>1</sub>. For example, 123 × 10<sup>-4</sup> is formatted as 0.0123
. <li>Case 0 ≤ e < 7: <ul> <li>Subcase i ≥ 0: d<sub>m</sub> is formatted as s<sub>1</sub>…s<sub>n</sub><!-- -->0
…0.0
, where there are exactly i zeroes between s<sub>n</sub> and the decimal point. For example, 123 × 10<sup>2</sup> is formatted as 12300.0
. <li>Subcase i < 0: d<sub>m</sub> is formatted as s<sub>1</sub>…<!-- -->s<sub>n+i</sub>.
<!-- -->s<sub>n+i+1</sub>…<!-- -->s<sub>n</sub>, where there are exactly -i digits to the right of the decimal point. For example, 123 × 10<sup>-1</sup> is formatted as 12.3
. </ul> <li>Case e < -3 or e ≥ 7: computerized scientific notation is used to format d<sub>m</sub>. Here e is formatted as by Integer#toString(int)
. <ul> <li>Subcase n = 1: d<sub>m</sub> is formatted as s<sub>1</sub>.0E
e. For example, 1 × 10<sup>23</sup> is formatted as 1.0E23
. <li>Subcase n > 1: d<sub>m</sub> is formatted as s<sub>1</sub>.
s<sub>2</sub><!-- -->…s<sub>n</sub>E
e. For example, 123 × 10<sup>-21</sup> is formatted as 1.23E-19
. </ul> </ul>
To create localized string representations of a floating-point value, use subclasses of java.text.NumberFormat
.
Java documentation for java.lang.Float.toString(float)
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.