Convert String value to Double Value Or Null - CSharp System

CSharp examples for System:Double

Description

Convert String value to Double Value Or Null

Demo Code


using System.Text;
using System.IO;//from www.j a v a2 s.co m
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        public static double? DoubleValueOrNull(this string value)
        {
            double result;
            if (double.TryParse(value, out result))
            {
                return result;
            }
            else
            {
                return null;
            }

        }
        public static double? DoubleValueOrNull(this string value, int decimals)
        {
            double? result = value.DoubleValueOrNull();
            if (result != null)
            {
                result = Math.Round(result.Value, decimals);
            }
            return result;
        }
}

Related Tutorials