private static Number UpTolevel(Number num, int level) { if (num == null) return 0; switch (level) { case NumInt: { if (num.TypeCode == NumberTypeCode.Int32) { return num; } return num; } case NumLong: { if (num.TypeCode == NumberTypeCode.Int64) { return num; } return num; } case NumBigInteger: { if (num.TypeCode == NumberTypeCode.BigInteger) { return num; } return BigInteger.Parse(num.ToString()); } case NumBigDecimal: { if (num.TypeCode == NumberTypeCode.BigDecimal) { return num; } return BigDecimal.Parse(num.ToString()); } default: { throw new ArgumentException("unsupported number level: " + level); } } }
public static string Format(Number value,NumberFormatInfo format) { if (value == null) return String.Empty; string[] numberGroupFormats = format.NumberGroupFormat.Split(","); string formattedNumber = ""; int wholeNumber = Math.Floor(Math.Abs(value)); string wholeNumberString = wholeNumber.ToString(); string decimalPartString = value.ToString().Substr(wholeNumberString.Length+1); int i = wholeNumberString.Length; int j = 0; while (i > 0) { int groupSize = int.Parse(numberGroupFormats[j]); // If there are more number group formats, get the next one, otherwise stick with this one until the end of the formatting if (j < (numberGroupFormats.Length-1)) j++; if (groupSize == 0) groupSize = i + 1; formattedNumber = wholeNumberString.Substring(i, i - groupSize) + formattedNumber; if (i > groupSize) formattedNumber = format.NumberSepartor + formattedNumber; i = i - groupSize; } // Add decimal part bool neg = (value < 0); if (format.Precision > 0) { // Add padding zeros int paddingRequired = format.Precision - decimalPartString.Length; for (int d = 0; d < paddingRequired; d++) { decimalPartString = decimalPartString + "0"; } formattedNumber = formattedNumber + format.DecimalSymbol + decimalPartString; } // Format negative if (neg) { switch (format.NegativeFormatCode) { case 0: formattedNumber = "(" + formattedNumber + ")"; break; case 2: formattedNumber = "- " + formattedNumber; break; case 3: formattedNumber = formattedNumber + "-"; break; case 4: formattedNumber = formattedNumber + " -"; break; case 1: default: formattedNumber = "-" + formattedNumber; break; } } return formattedNumber; }
static void AreEqual (string expected, Number actual, string msg) { expected = expected.Replace (" ", "").ToLower ().TrimStart ('0'); Assert.AreEqual (expected, actual.ToString (16), msg); }
protected override Number VisitNumber(Number node) { visitedNumber = true; Assert.IsTrue(node.ToString() == "0" || node.ToString() == "-0.0e0"); return base.VisitNumber(node); }
public string ToString(string format) { if (string.IsNullOrEmpty(format)) { return(ToString()); } Number value = this; if (format == "P" || format == "p") { value = value.As <double>() * 100; format = "0.00%"; } else if (format[0] == 'X') { var radix = 16; var remainingFormat = format.Substring(1); var s = this.As <JsNumber>().toString(radix); if (remainingFormat.Length > 0) { var minimumDigits = int.Parse(remainingFormat); while (s.length < minimumDigits.As <JsNumber>()) { s = "0" + s; } } return(s); } var parts = value.ToString().Split('.'); var leftOfDecimal = parts[0]; var rightOfDecimal = parts.Length > 1 ? parts[1] : ""; var leftDigits = new Queue <char>(leftOfDecimal); var rightDigits = new Queue <char>(rightOfDecimal); var result = new StringBuilder(); if (value.As <JsNumber>() < 0) { result.Append("-"); } var state = DecimalState.Before; foreach (var token in format) { switch (token) { case '0': case '#': switch (state) { case DecimalState.Before: if (leftDigits.Count > 0) { result.Append(leftDigits.Dequeue()); } else if (token == '0') { result.Append('0'); } break; case DecimalState.On: state = DecimalState.After; if ((rightDigits.Count == 0 && token == '0') || rightDigits.Count > 0) { result.Append('.'); } if (rightDigits.Count == 0 && token == '0') { result.Append('0'); } else if (rightDigits.Count > 0) { result.Append(rightDigits.Dequeue()); } break; case DecimalState.After: if (rightDigits.Count == 0 && token == '0') { result.Append('0'); } else if (rightDigits.Count > 0) { result.Append(rightDigits.Dequeue()); } break; } break; case '.': state = DecimalState.On; while (leftDigits.Count > 0) { result.Append(leftDigits.Dequeue()); } break; } } return(result.ToString()); }
/// <summary> /// Cálculos para la comprobación del Cif (Entidad jurídica) /// </summary> private bool CheckDocumentCif() { string[] letrasCodigo = { "J", "A", "B", "C", "D", "E", "F", "G", "H", "I" }; var n = Number.ToString("0000000"); var sumaPares = 0; var sumaImpares = 0; var sumaTotal = 0; var i = 0; var retVal = false; // Recorrido por todos los dígitos del número for (i = 0; i < n.Length; i++) { int.TryParse(n[i].ToString(), out var aux); if ((i + 1) % 2 == 0) { // Si es una posición par, se suman los dígitos sumaPares += aux; } else { // Si es una posición impar, se multiplican los dígitos por 2 aux = aux * 2; // se suman los dígitos de la suma sumaImpares += SumDigits(aux); } } // Se suman los resultados de los números pares e impares sumaTotal += sumaPares + sumaImpares; // Se obtiene el dígito de las unidades var unidades = sumaTotal % 10; // Si las unidades son distintas de 0, se restan de 10 if (unidades != 0) { unidades = 10 - unidades; } switch (LetraInicial) { // Sólo números case "A": case "B": case "E": case "H": retVal = DigitoControl == unidades.ToString(); break; // Sólo letras case "K": case "P": case "Q": case "S": retVal = DigitoControl == letrasCodigo[unidades]; break; default: retVal = DigitoControl == unidades.ToString() || DigitoControl == letrasCodigo[unidades]; break; } return(retVal); }