Advertisement
flameonik

Untitled

Oct 18th, 2021 (edited)
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. // convertNumeralSystem
  2.  
  3. // on top
  4.         String originalValue = "";
  5.         String originalFractionValue = "";
  6.         double valueInFractionDecimal = 0; 
  7.  
  8. // before converting   
  9.         // Divide value into integer and fractional parts.
  10.         int commaPosition = original.getValue().IndexOf(",");
  11.         if (commaPosition == -1)
  12.         {
  13.             originalValue = original.getValue();
  14.             originalFractionValue = "0";
  15.         }
  16.         else
  17.         {
  18.             originalValue = original.getValue().SubString(0, commaPosition);
  19.             originalFractionValue = original.getValue().SubString(commaPosition + 1);
  20.         }
  21.  
  22. // before returning
  23.         // Convert fraction and add that to the target value.
  24.         if (valueInFractionDecimal != 0)
  25.         {
  26.             targetValue += ",";
  27.             int index = 1;
  28.  
  29.             while (valueInFractionDecimal != 0)
  30.             {
  31.                 if (Math.Truncate(valueInFractionDecimal * targetSys) <= 9)
  32.                 {
  33.                     targetValue += (Math.Truncate(valueInFractionDecimal * targetSys)).ToString();
  34.                 }
  35.                 else
  36.                 {
  37.                     char letterFromNumber = (char)(Math.Truncate(valueInFractionDecimal * targetSys) + 55);
  38.                     targetValue += letterFromNumber.ToString();
  39.                 }
  40.  
  41.                 valueInFractionDecimal *= targetSys;
  42.                 valueInFractionDecimal -= Math.Floor(valueInFractionDecimal);
  43.  
  44.                 // Break
  45.                 if (++index == 10)
  46.                 {
  47.                     targetValue = targetValue.TrimEnd('0');
  48.                     break;
  49.                 }
  50.             }
  51.         }
  52.  
  53. // convertToDecimalValue
  54.  
  55. // inside if
  56.             valueInFractionDecimal = Double.Parse(originalFractionValue);
  57.             double x = Math.Pow(10, originalFractionValue.Length);
  58.             valueInFractionDecimal /= x;
  59.  
  60. // at the bottom of else
  61.             index = -1;
  62.  
  63.             // Iterating through numbers of fraction
  64.             for (int i = 0; i < originalFractionValue.Length; i++)
  65.             {
  66.                 // Establishing value of current character
  67.  
  68.                 double symbolValue = 0;
  69.                 byte asciiCode = Encoding.Default.GetBytes(originalFractionValue)[i];
  70.  
  71.                 // In case of number
  72.                 if (asciiCode >= 48 && asciiCode <= 57)
  73.                 {
  74.                     symbolValue = (double)(asciiCode - 48);
  75.                 }
  76.                 // In case of upper case letter
  77.                 else if (asciiCode >= 65 && asciiCode <= 90)
  78.                 {
  79.                     symbolValue = (double)(asciiCode - 55);
  80.                 }
  81.                 // In case of lower case letter
  82.                 else if (asciiCode >= 97 && asciiCode <= 122)
  83.                 {
  84.                     symbolValue = (double)(asciiCode - 87);
  85.                 }
  86.  
  87.                 valueInFractionDecimal += symbolValue * Math.Pow((double)number.system, (double)index);
  88.                 index--;
  89.             }
  90.  
  91. // validator
  92.  
  93. // checkIfNumberExistsInGivenSystem
  94.  
  95. // before other caracter case
  96.             // In case of comma or dot
  97.             else if (asciiCode == 44 || asciiCode == 46)
  98.             {
  99.                 if (i == 0)
  100.                 {
  101.                     this.displayInfo("Comma/dot can't be inserted at the beginning of a number", true);
  102.                     return false;
  103.                 }
  104.                 else if (dotAlready)
  105.                 {
  106.                     this.displayInfo("Comma/dot can't be inserted twice", true);
  107.                     return false;
  108.                 }
  109.                 dotAlready = true;
  110.             }
  111.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement