using System; class Sample { public static void Main() { Console.WriteLine("(D) Decimal:. . . . . . . . . {0:D} " + -123, -123.45f); } }
Using Floats
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; namespace Client.Chapter_1___Common_Type_System { public class UsingFloatsChapter_1___Common_Type_System { static void Main(string[] args) { float MyFloat = 3.281f; double MyDouble = 5E-02; } } }
Int and float value
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class IntFloatTester { public static void Main() { int smallInt = 5; int largeInt = 12; int intQuotient; intQuotient = largeInt / smallInt; Console.WriteLine("Dividing integers. {0} / {1} = {2}", largeInt, smallInt, intQuotient); float smallFloat = 5; float largeFloat = 12; float FloatQuotient; FloatQuotient = largeFloat / smallFloat; Console.WriteLine("Dividing floats. {0} / {1} = {2}", largeFloat, smallFloat, FloatQuotient); } }
Format a float number in ToString method
using System; public class MainClass { public static void Main() { Console.WriteLine(10.3.ToString("C", null)); // $10.30 } }
Get Enum Description
//http://facebooktoolkit.codeplex.com/ //http://facebooktoolkit.codeplex.com/license using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Text; namespace Facebook.Utility { ///<summary> /// Contains helper functions used to help when using enums inside of collections and serialization ///</summary> public class EnumHelper { private EnumHelper() { } ///<summary> ///</summary> ///<param name="enumeratedType"></param> ///<typeparam name="T"></typeparam> ///<returns></returns> ///<exception cref="ArgumentException"></exception> public static string GetEnumDescription<T>(T enumeratedType) { var description = enumeratedType.ToString(); var enumType = typeof (T); // Can't use type constraints on value types, so have to do check like this if (enumType.BaseType != typeof (Enum)) throw new ArgumentException("T must be of type System.Enum"); var fieldInfo = enumeratedType.GetType().GetField(enumeratedType.ToString()); if (fieldInfo != null) { var attributes = fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { description = ((DescriptionAttribute) attributes[0]).Description; } } return description; } ///<summary> ///</summary> ///<param name="enums"></param> ///<typeparam name="T"></typeparam> ///<returns></returns> ///<exception cref="ArgumentException"></exception> public static string GetEnumCollectionDescription<T>(Collection<T> enums) { var sb = new StringBuilder(); var enumType = typeof (T); // Can't use type constraints on value types, so have to do check like this if (enumType.BaseType != typeof (Enum)) throw new ArgumentException("T must be of type System.Enum"); foreach (var enumeratedType in enums) { sb.AppendLine(GetEnumDescription(enumeratedType)); } return sb.ToString(); } } }
Enum To Array
#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TextGlow.Control.Utilities
{
public static class EnumUtils
{
public static void sdf()
{
}
public static T[] EnumToArray
{
//get the enumeration type
Type et = typeof(T);
T defaultValue = default(T);
//get the public static fields (members of the enum)
FieldInfo[] fi = et.GetFields(BindingFlags.Static | BindingFlags.Public);
//create a new enum array
T[] values = new T[fi.Length];
//populate with the values
for (int i = 0; i < fi.Length; i++)
{
values[i] = (T)fi[i].GetValue(defaultValue);
}
//return the array
return values;
}
}
}
[/csharp]
Try to parse Enum
namespace System { public static class EnumExtensions { /// <summary> /// Tries the parse. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="theEnum">The enum.</param> /// <param name="valueToParse">The value to parse.</param> /// <param name="returnValue">The return value.</param> /// <returns></returns> public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue) { returnValue = default(T); int intEnumValue; if (Int32.TryParse(valueToParse, out intEnumValue)) { if (Enum.IsDefined(typeof (T), intEnumValue)) { returnValue = (T) (object) intEnumValue; return true; } } return false; } } }