Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Strings: Padding Integers and Handling Decimal Float Numbers
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { // Ways to convert int to string Console.WriteLine(3.ToString()); Console.WriteLine(Convert.ToString(43).PadLeft(4,'0')); Console.WriteLine(43.ToString("D4")); Console.WriteLine(string.Format("{0:000}",45)); Console.WriteLine(); // converting float to string Console.WriteLine(3.35.ToString()); Console.WriteLine(String.Format("{0} -> {1:0.00}", 123.45678, 123.45678)); // gives 2 decimal places Console.WriteLine(String.Format("{0} -> {1:0.00}", 123, 123)); // still gives 2 decimal places 123.00 even when there are no decimals Console.WriteLine(); Console.WriteLine(string.Format("{0} -> {1:0.##}", 123.456, 123.456)); // gives 2 decimal places only when there are decimal digits Console.WriteLine(string.Format("{0} -> {1:0.##}", 123, 123)); // gives 123 without decimals because # denotes optional Console.WriteLine(); // gives 123 without decimal places because after rounding to 2 decimals places result is 123.00 Console.WriteLine(string.Format("{0} -> {1:0.##}", 123.001234, 123.001234)); // gives 123.01 because after rounding last decimal point, 2 decimal places is not 123.00 any more Console.WriteLine(string.Format("{0} -> {1:0.##}", 123.007234, 123.007234)); Console.WriteLine(string.Format("{0} -> {1:0.0#}", 123.00, 123.00)); // formats 1 mandatory decimal place, the other is optional } } }
run
|
edit
|
history
|
help
0
defalt branch name
towers of hanoi
Abstract Method Design Pattern in C#
Contravariance
Find smallest and largest integers in unsorted array
fnaf
Math Main
line 1 syntax error
Concurrent bag ex
EFDCSX ASXS