using System; // Mixing types in expressions class MixedTypes { static void Main() { int x = 3; float y = 4.5f; short z = 5; double w = 1.7E+3; // Result of the 2nd argument is a double: Console.WriteLine("The sum is {0}", x + y + z + w); } }
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Automatic conversion from double to string
using System; class Plus { static void Main() { Console.WriteLine(+5); // unary plus Console.WriteLine(5 + 5); // addition Console.WriteLine(5 + .5); // addition Console.WriteLine("5" + "5"); // string concatenation Console.WriteLine(5.0 + "5"); // string concatenation // note automatic conversion from double to string } }
Get Decimal Places
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{
public static int GetDecimalPlaces(double value)
{
// increasing max decimal places above 10 produces weirdness
int maxDecimalPlaces = 10;
double threshold = Math.Pow(0.1d, maxDecimalPlaces);
if (value == 0.0)
return 0;
int decimalPlaces = 0;
while (value – Math.Floor(value) > threshold && decimalPlaces < maxDecimalPlaces)
{
value *= 10.0;
decimalPlaces++;
}
return decimalPlaces;
}
}
}
[/csharp]
Format double value
using System; public class NumParsingApp { public static void Main(string[] args) { int i = int.Parse("12345"); Console.WriteLine("i = {0}", i); int j = Int32.Parse("12345"); Console.WriteLine("j = {0}", j); double d = Double.Parse("1.2345E+6"); Console.WriteLine("d = {0:F}", d); string s = i.ToString(); Console.WriteLine("s = {0}", s); } }
double number format: 0:C, 0:D9, 0:E, 0:F3, 0:N, 0:X, 0:x
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("C format: {0:C}", 99989.987); Console.WriteLine("D9 format: {0:D9}", 99999); Console.WriteLine("E format: {0:E}", 99999.76543); Console.WriteLine("F3 format: {0:F3}", 99999.9999); Console.WriteLine("N format: {0:N}", 99999); Console.WriteLine("X format: {0:X}", 99999); Console.WriteLine("x format: {0:x}", 99999); } }
Epsilon, PositiveInfinity, NegativeInfinity, MaxValue, MinValue
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("-> double.Epsilon: {0}", double.Epsilon); Console.WriteLine("-> double.PositiveInfinity: {0}", double.PositiveInfinity); Console.WriteLine("-> double.NegativeInfinity: {0}", double.NegativeInfinity); Console.WriteLine("-> double.MaxValue: {0}", double.MaxValue); Console.WriteLine("-> double.MinValue: {0}", double.MinValue); } }
converts Fahrenheit to Celsius
/* This program converts Fahrenheit to Celsius. Call this program FtoC.cs. */ using System; public class FtoC { public static void Main() { double f; // holds the temperature in Fahrenheit double c; // holds the temparture in Celsius f = 59.0; // start with 59 degrees Fahrenheit c = 5.0 / 9.0 * (f - 32.0); // convert to Celsius Console.Write(f + " degrees Fahrenheit is "); Console.WriteLine(c + " degrees Celsius."); } }