Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit d5a95d6

Browse files
committed
Add safe string conversion overloads to other popular number types
1 parent fd09ca9 commit d5a95d6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,39 @@ public static long ToInt64(this string text, long defaultValue)
801801
return Int64.TryParse(text, out ret) ? ret : defaultValue;
802802
}
803803

804+
public static float ToFloat(this string text)
805+
{
806+
return text == null ? default(float) : float.Parse(text);
807+
}
808+
809+
public static float ToFloat(this string text, float defaultValue)
810+
{
811+
float ret;
812+
return float.TryParse(text, out ret) ? ret : defaultValue;
813+
}
814+
815+
public static double ToDouble(this string text)
816+
{
817+
return text == null ? default(double) : double.Parse(text);
818+
}
819+
820+
public static double ToDouble(this string text, double defaultValue)
821+
{
822+
double ret;
823+
return double.TryParse(text, out ret) ? ret : defaultValue;
824+
}
825+
826+
public static decimal ToDecimal(this string text)
827+
{
828+
return text == null ? default(decimal) : decimal.Parse(text);
829+
}
830+
831+
public static decimal ToDecimal(this string text, decimal defaultValue)
832+
{
833+
decimal ret;
834+
return decimal.TryParse(text, out ret) ? ret : defaultValue;
835+
}
836+
804837
public static bool Glob(this string value, string pattern)
805838
{
806839
int pos;

0 commit comments

Comments
 (0)