Skip to content

Instantly share code, notes, and snippets.

@cybrox
Last active August 29, 2015 13:57
Show Gist options
  • Save cybrox/9505209 to your computer and use it in GitHub Desktop.
Save cybrox/9505209 to your computer and use it in GitHub Desktop.
Simple class for converting numbers without crashing the program for educational stuff.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace CyLib {
/**
* @class Cynum
* @name Cynum
* @desc Convert different number input and output
* using System.Text.RegularExpressions
* @author Sven Gehring
* @copyright 2014+ Sven Gehring
*/
public class Cynum {
private Regex patternInteger = new Regex(@"[0-9]{1,}"); // Match a stirng of numbers
private Regex patternDouble = new Regex(@"\.?[0-9]{1,}(\.[0-9]{1,})?"); // Match a string of numbers with or without dots
private Regex patternEngnum = new Regex(@"\.?[0-9]{1,}[A-z]?([0-9]{1,})?"); // Match a string of numbers, characters and dots
private Regex patternEngchar = new Regex(@"(f|p|n|u|m|k|M|G){1}"); // Match the allowed suffix characters
/**
* @method getInteger
* @name Get Integer
* @alias 'Ganzzahl'
* @desc Try to convert an input string to an integer
* @param {string} input - The given input string
* @return {int} output - The converted integer
*/
public int getInteger(string input) {
int output;
if (this.patternInteger.IsMatch(input)) {
if (Int32.TryParse(input, out output)) {
return output;
}
}
return 0;
}
/**
* @method getDouble
* @name Get Double
* @alias 'Bruchzahl'
* @desc Try to convert an input string to a double
* @param {string} input - The given input string
* @return {double} output - The converted double
*/
public double getDouble(string input) {
double output;
if (this.patternDouble.IsMatch(input)) {
if (Double.TryParse(input, out output)) {
return output;
}
}
return 0;
}
/**
* @method getEngnum
* @name Get Engineering number
* @alias 'Engnum'
* @desc Try to convert an engineering number to a double
* @param {string} input - The given input string
* @return {double} output - The converted double
*/
public double getEngnum(string input) {
double output;
char suffix = 'x';
char[] suffixlist = { 'f', 'p', 'n', 'u', 'm', 'k', 'M', 'G', 'x' };
double[] multiplier = { 0.000000000000001, 0.000000000001, 0.000000001, 0.000001, 0.001, 1000, 1000000, 1000000000, 1 };
if (this.patternEngnum.IsMatch(input)) {
foreach (Match match in this.patternEngchar.Matches(input)) {
suffix = Convert.ToChar(match.Value);
}
input = Regex.Replace(input, @"(f|p|n|u|m|k|M|G){1}", ".");
input = input.TrimEnd('.');
output = this.getDouble(input);
output *= multiplier[(Array.IndexOf(suffixlist, suffix))];
return output;
}
return 0;
}
/**
* @method setEngnum
* @name Set Engineering number
* @alias 'EngAusgabe'
* @desc Try to convert a double into an engineering number
* @param {double} input - The given double
* @return {string} output - The converted engineering string
*/
public string setEngnum(double input) {
string output = "";
char[] suffixlist = { 'f', 'p', 'n', 'u', 'm', ' ', 'k', 'M', 'G' };
double[] multiplier = { 0.000000000000001, 0.000000000001, 0.000000001, 0.000001, 0.001, 1, 1000, 1000000, 1000000000, Double.PositiveInfinity };
for (int i = 0; i < 9; i++) {
if (input >= multiplier[i] && input < multiplier[(i + 1)]) {
output = Convert.ToString(input / multiplier[i]) + suffixlist[i];
}
}
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment