Created
September 29, 2017 18:49
-
-
Save dsoverby1986/46e7a8df927cb09372f9e7c7dabe1833 to your computer and use it in GitHub Desktop.
Number formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("This program will take a number to be formatted and another number that\nspecifies the number of decimal places the formatted number should have.\n"); | |
double num = (double)GetNumber(0); | |
int dPlaces = (int)GetDecimalPlaces(0); | |
Console.WriteLine($"\nYour number, after formatting: {FormatNumber(num, dPlaces)}"); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
static object GetNumber(int attempts) | |
{ | |
if (attempts > 0) | |
Console.WriteLine("\nYour input is invalid. Try again.\n"); | |
Console.WriteLine("Enter a number...\n"); | |
double num; | |
if (double.TryParse(Console.ReadLine(), out num)) | |
return num; | |
return GetNumber(++attempts); | |
} | |
static object GetDecimalPlaces(int attempts) | |
{ | |
if (attempts > 0) | |
Console.WriteLine("\nYour input is invalid. Try again."); | |
Console.WriteLine("\nEnter the number of decimal places...\n"); | |
int num; | |
if (Int32.TryParse(Console.ReadLine(), out num)) | |
return num; | |
return GetDecimalPlaces(++attempts); | |
} | |
static string FormatNumber(double num, int dPlaces) | |
{ | |
string nStr = num.ToString(); | |
bool positive = nStr.IndexOf('-') == -1; | |
if (!positive) | |
nStr = nStr.Substring(1); | |
int decimalSet = (int)Math.Pow(10, dPlaces); | |
nStr = (Math.Round(double.Parse(nStr.Replace(",", "")) * decimalSet) / decimalSet).ToString(); | |
int decimalIndex = nStr.IndexOf('.'); | |
string decimalValue = decimalIndex != -1 ? nStr.Substring(decimalIndex) : ""; | |
nStr = decimalIndex == -1 ? nStr : nStr.Substring(0, decimalIndex); | |
int numLength = nStr.Length; | |
int leadingGroup = numLength % 3; | |
List<string> returnNumList = new List<string>(); | |
string returnNum = ""; | |
if (leadingGroup > 0) | |
returnNumList.Add(nStr.Substring(0, leadingGroup)); | |
for (int i = leadingGroup; i < numLength; i += 3) | |
returnNumList.Add(nStr.Substring(i, 3)); | |
returnNum = String.Join(",", returnNumList); | |
if (!string.IsNullOrWhiteSpace(decimalValue)) | |
returnNum += decimalValue; | |
if (decimalValue.Length < (dPlaces + 1)) | |
{ | |
if (returnNum.IndexOf('.') == -1) | |
returnNum += "."; | |
returnNum += new string('0', dPlaces - (decimalValue.Length - 1)); | |
} | |
if (!positive) | |
if (returnNum[0] == ',') | |
returnNum.Replace(returnNum[0], '-'); | |
else | |
returnNum = '-' + returnNum; | |
if (double.IsNaN(double.Parse(returnNum.Replace(",", "")))) | |
returnNum = "0.00"; | |
return returnNum; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment