Add ‘decimal-mark’ thousands separators to a number

If you want to add a thousands separator, you can write: >>> ‘{0:,}’.format(1000000) ‘1,000,000’ But it only works in Python 2.7 and above. See format string syntax. In older versions, you can use locale.format(): >>> import locale >>> locale.setlocale(locale.LC_ALL, ”) ‘en_AU.utf8’ >>> locale.format(‘%d’, 1000000, 1) ‘1,000,000’ the added benefit of using locale.format() is that it … Read more

What is the easiest way to pad a string with 0 to the left?

The fmt module documentation describes all the formatting options: Fill / Alignment The fill character is provided normally in conjunction with the width parameter. This indicates that if the value being formatted is smaller than width some extra characters will be printed around it. The extra characters are specified by fill, and the alignment can … Read more

Phone number formatting on iOS

This will help you Format (xxx) xxx-xxxx – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { int length = (int)[self getLength:textField.text]; //NSLog(@”Length = %d “,length); if(length == 10) { if(range.length == 0) return NO; } if(length == 3) { NSString *num = [self formatNumber:textField.text]; textField.text = [NSString stringWithFormat:@”(%@) “,num]; if(range.length > 0) textField.text = [NSString stringWithFormat:@”%@”,[num substringToIndex:3]]; … Read more

How do I convert String to Number according to locale (opposite of .toLocaleString)?

I think you are looking for something like: https://github.com/jquery/globalize Above link will take you to git project page. This is a js library contributed by Microsoft. You should give it one try and try to use formt method of that plugin. If you want to study this plugin, here is the link for the same: … Read more

Format telephone and credit card numbers in AngularJS

Also, if you need to format telephone number on output only, you can use a custom filter like this one: angular.module(‘ng’).filter(‘tel’, function () { return function (tel) { if (!tel) { return ”; } var value = tel.toString().trim().replace(/^\+/, ”); if (value.match(/[^0-9]/)) { return tel; } var country, city, number; switch (value.length) { case 10: // … Read more

In jQuery, what’s the best way of formatting a number to 2 decimal places?

If you’re doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here’s the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field. You may want something different. <script type=”text/javascript”> // … Read more

Format currency without currency symbol

The following works. It’s a bit ugly, but it fulfils the contract: NumberFormat nf = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(“”); ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols); System.out.println(nf.format(12345.124).trim()); You could also get the pattern from the currency format, remove the currency symbol, and reconstruct a new format from the new pattern: NumberFormat nf = NumberFormat.getCurrencyInstance(); String pattern = … Read more

How to Automatically add thousand separators as number is input in EditText

Even-though It’s late. Intended for future visitors. Fetures of the following codes Puts thousand separator in EditText as it’s text changes. adds 0. Automatically when pressed period (.) At First. Ignores 0 input at Beginning. Just copy the following Class named NumberTextWatcherForThousand which implements TextWatcher import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import java.util.StringTokenizer; /** * … Read more

How do I format a number with a variable number of digits in Python?

If you are using it in a formatted string with the format() method which is preferred over the older style ”% formatting >>> ‘One hundred and twenty three with three leading zeros {0:06}.’.format(123) ‘One hundred and twenty three with three leading zeros 000123.’ See http://docs.python.org/library/stdtypes.html#str.format http://docs.python.org/library/string.html#formatstrings Here is an example with variable width >>> ‘{num:0{width}}’.format(num=123, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)