public static Number Parse(string value, NumberFormatInfo format) { if (String.IsNullOrEmpty(value)) return null; // Remove spaces value = value.Replace(" ", ""); // Remove groupseparators and substitute the decimal separator if (format.DecimalSymbol != ".") { value = value.Replace(format.DecimalSymbol, "."); } value = value.Replace(format.NumberSepartor, ""); // Remove negative formatting if (value.StartsWith("(")) { value = "-" + value.Replace("(", "").Replace(")", ""); } else if (value.EndsWith("-")) { value = "-" + value.Substring(0, value.Length - 1); } Number numericValue = Number.Parse(value); return numericValue; }
private static string FormatNumber(Money value, NumberFormatInfo format) { if (value != null) { return NumberEx.Format(value.Value,format); } else { return String.Empty; } }
public XrmNumberEditor(EditorArguments args) : base(args) { numberFormatInfo = (NumberFormatInfo)args.Column.Options; input = jQuery.FromHtml("<INPUT type=text class='editor-text' />") .AppendTo(args.Container) .Bind("keydown.nav",delegate(jQueryEvent e){ if (e.Which == 37 || e.Which == 39 ) //LEFT or RIGHT{ e.StopImmediatePropagation(); }) .Focus() .Select(); }
public static NumberFormatInfo GetCurrencyEditFormatInfo() { NumberFormatInfo format = new NumberFormatInfo(); if (OrganizationServiceProxy.UserSettings != null) { format.DecimalSymbol = OrganizationServiceProxy.UserSettings.DecimalSymbol; format.NumberGroupFormat = OrganizationServiceProxy.UserSettings.NumberGroupFormat; format.NumberSepartor = OrganizationServiceProxy.UserSettings.NumberSeparator; format.NegativeFormatCode = OrganizationServiceProxy.UserSettings.NegativeCurrencyFormatCode; format.Precision = OrganizationServiceProxy.UserSettings.CurrencyDecimalPrecision.Value; format.CurrencySymbol = OrganizationServiceProxy.UserSettings.CurrencySymbol; } else { format.DecimalSymbol = "."; format.NumberGroupFormat = "3"; format.NumberSepartor = ","; format.NegativeFormatCode = 0; format.Precision = 2; format.CurrencySymbol = "$"; } return format; }
public static NumberFormatInfo GetNumberFormatInfo() { NumberFormatInfo format = new NumberFormatInfo(); if (OrganizationServiceProxy.UserSettings != null) { format.DecimalSymbol = OrganizationServiceProxy.UserSettings.DecimalSymbol; format.NumberGroupFormat = OrganizationServiceProxy.UserSettings.NumberGroupFormat; format.NumberSepartor = OrganizationServiceProxy.UserSettings.NumberSeparator; format.NegativeFormatCode = OrganizationServiceProxy.UserSettings.NegativeFormatCode; } else { format.DecimalSymbol = "."; format.NumberGroupFormat = "3"; format.NumberSepartor = ","; format.NegativeFormatCode = 0; } // defaults format.Precision = 2; format.MinValue = -2147483648; format.MaxValue = 2147483648; return format; }
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; }
private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, string value, NumberFormatInfo format) { Observable<Money> observable = (Observable<Money>)valueAccessor(); bool isValid = true; Number numericValue = NumberEx.Parse(value, format); if (!Number.IsNaN(numericValue) && numericValue>=format.MinValue && numericValue<=format.MaxValue) { // Set to precision numericValue = NumberEx.Round(numericValue, format.Precision); Money newValue = new Money((decimal)numericValue); observable.SetValue(newValue); if (((string)Script.Literal("typeof({0}.isValid)", observable)) != "undefined") { isValid = ((IValidatedObservable)observable).IsValid() == true; } if (isValid) { string formattedNumber = FormatNumber(newValue, format); inputField.Value(formattedNumber); inputField.Blur(); } } else { Script.Alert(String.Format("You must enter a number between {0} and {1}",format.MinValue,format.MaxValue)); Money currentValue = observable.GetValue(); string formattedNumber = FormatNumber(currentValue, format); inputField.Value(formattedNumber); inputField.Focus(); } }