Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // convertNumeralSystem
- // on top
- String originalValue = "";
- String originalFractionValue = "";
- double valueInFractionDecimal = 0;
- // before converting
- // Divide value into integer and fractional parts.
- int commaPosition = original.getValue().IndexOf(",");
- if (commaPosition == -1)
- {
- originalValue = original.getValue();
- originalFractionValue = "0";
- }
- else
- {
- originalValue = original.getValue().SubString(0, commaPosition);
- originalFractionValue = original.getValue().SubString(commaPosition + 1);
- }
- // before returning
- // Convert fraction and add that to the target value.
- if (valueInFractionDecimal != 0)
- {
- targetValue += ",";
- int index = 1;
- while (valueInFractionDecimal != 0)
- {
- if (Math.Truncate(valueInFractionDecimal * targetSys) <= 9)
- {
- targetValue += (Math.Truncate(valueInFractionDecimal * targetSys)).ToString();
- }
- else
- {
- char letterFromNumber = (char)(Math.Truncate(valueInFractionDecimal * targetSys) + 55);
- targetValue += letterFromNumber.ToString();
- }
- valueInFractionDecimal *= targetSys;
- valueInFractionDecimal -= Math.Floor(valueInFractionDecimal);
- // Break
- if (++index == 10)
- {
- targetValue = targetValue.TrimEnd('0');
- break;
- }
- }
- }
- // convertToDecimalValue
- // inside if
- valueInFractionDecimal = Double.Parse(originalFractionValue);
- double x = Math.Pow(10, originalFractionValue.Length);
- valueInFractionDecimal /= x;
- // at the bottom of else
- index = -1;
- // Iterating through numbers of fraction
- for (int i = 0; i < originalFractionValue.Length; i++)
- {
- // Establishing value of current character
- double symbolValue = 0;
- byte asciiCode = Encoding.Default.GetBytes(originalFractionValue)[i];
- // In case of number
- if (asciiCode >= 48 && asciiCode <= 57)
- {
- symbolValue = (double)(asciiCode - 48);
- }
- // In case of upper case letter
- else if (asciiCode >= 65 && asciiCode <= 90)
- {
- symbolValue = (double)(asciiCode - 55);
- }
- // In case of lower case letter
- else if (asciiCode >= 97 && asciiCode <= 122)
- {
- symbolValue = (double)(asciiCode - 87);
- }
- valueInFractionDecimal += symbolValue * Math.Pow((double)number.system, (double)index);
- index--;
- }
- // validator
- // checkIfNumberExistsInGivenSystem
- // before other caracter case
- // In case of comma or dot
- else if (asciiCode == 44 || asciiCode == 46)
- {
- if (i == 0)
- {
- this.displayInfo("Comma/dot can't be inserted at the beginning of a number", true);
- return false;
- }
- else if (dotAlready)
- {
- this.displayInfo("Comma/dot can't be inserted twice", true);
- return false;
- }
- dotAlready = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement