c# - Parsing pair of floats -
given string like: "0.123, 0.456"
simplest way parse 2 float values 2 variables a
, b
?
i suggest:
- split on comma (
string.split
) - trim (
string.trim
) - parse either
float.parse
orfloat.tryparse
. (if want exception thrown if format incorrect, goparse
. if want handle parsing failures part of normal control flow, usetryparse
.)
if numbers going in format, explicitly specify cultureinfo.invariantculture
. consider using decimal
(or double
) instead of float
.
Comments
Post a Comment