number-formatting

Format Number like Stack Overflow (rounded to thousands with K suffix)

自古美人都是妖i 提交于 2019-11-26 15:14:40
How to format numbers like SO with C#? 10, 5k, ... Like this: ( EDIT : Tested) static string FormatNumber(int num) { if (num >= 100000) return FormatNumber(num / 1000) + "K"; if (num >= 10000) { return (num / 1000D).ToString("0.#") + "K"; } return num.ToString("#,0"); } Examples: 1 => 1 23 => 23 136 => 136 6968 => 6,968 23067 => 23.1K 133031 => 133K Note that this will give strange values for numbers >= 10 8 . For example, 12345678 becomes 12.3KK . The code below is tested up to int.MaxValue This is not the most beautiful code but is most efficient. But you can use it as: 123.KiloFormat();

Phone number formatting on iOS

谁说胖子不能爱 提交于 2019-11-26 14:05:32
I have a text field where the user enters data. It's a phone number field. If the user enters 1234567890 , I want it displayed as (123)-(456)-7890 as the user types. How is this possible? wan 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

What's the easiest way to add commas to an integer? [duplicate]

北慕城南 提交于 2019-11-26 13:48:46
问题 Possible Duplicate: How to print number with commas as thousands separators? For example: >> print numberFormat(1234) >> 1,234 Or is there a built-in function in Python that does this? 回答1: No one so far has mentioned the new ',' option which was added in version 2.7 to the Format Specification Mini-Language -- see PEP 378: Format Specifier for Thousands Separator in the What's New in Python 2.7 document. It's easy to use because you don't have to mess around with locale (but is limited for

PHP How do I round down to two decimal places?

亡梦爱人 提交于 2019-11-26 13:22:43
I need to round down a decimal in PHP to two decimal places so that: 49.955 becomes... 49.95 I have tried number_format , but this just rounds the value to 49.96 . I cannot use substr because the number may be smaller (such as 7.950). I've been unable to find an answer to this so far. Any help much appreciated. This can work: floor($number * 100) / 100 Here is a nice function that does the trick without using string functions: <?php function floorp($val, $precision) { $mult = pow(10, $precision); // Can be cached in lookup table return floor($val * $mult) / $mult; } print floorp(49.955, 2); ?>

Format telephone and credit card numbers in AngularJS

泄露秘密 提交于 2019-11-26 11:52:51
问题 Question one (formatting telephone number): I\'m having to format a telephone number in AngularJS but there is no filter for it. Is there a way to use filter or currency to format 10 digits to (555) 555-5255 ? and still preserve the data type of the field as integer? Question two (masking credit card number): I have a credit card field that is mapped to AngularJS, like: <input type=\"text\" ng-model=\"customer.creditCardNumber\"> which is returning the whole number ( 4111111111111111 ). I

How to format numbers by prepending 0 to single-digit numbers?

…衆ロ難τιáo~ 提交于 2019-11-26 11:34:18
I want to format a number to have two digits. The problem is caused when 0 – 9 is passed, so I need it to be formatted to 00 – 09 . Is there a number formatter in JavaScript? Joseph Marikle The best method I've found is something like the following: (Note that this simple version only works for positive integers) var myNumber = 7; var formattedNumber = ("0" + myNumber).slice(-2); console.log(formattedNumber); For decimals, you could use this code (it's a bit sloppy though). var myNumber = 7.5; var dec = myNumber - Math.floor(myNumber); myNumber = myNumber - dec; var formattedNumber = ("0" +

Java Currency Number format

 ̄綄美尐妖づ 提交于 2019-11-26 10:27:20
Is there a way to format a decimal as following: 100 -> "100" 100.1 -> "100.10" If it is a round number, omit the decimal part. Otherwise format with two decimal places. extraneon I doubt it. The problem is that 100 is never 100 if it's a float, it's normally 99.9999999999 or 100.0000001 or something like that. If you do want to format it that way, you have to define an epsilon, that is, a maximum distance from an integer number, and use integer formatting if the difference is smaller, and a float otherwise. Something like this would do the trick: public String formatDecimal(float number) {

Format currency without currency symbol

青春壹個敷衍的年華 提交于 2019-11-26 09:32:00
问题 I am using NumberFormat.getCurrencyInstance(myLocale) to get a custom currency format for a locale given by me. However, this always includes the currency symbol which I don\'t want, I just want the proper currency number format for my given locale without the currency symbol. Doing a format.setCurrencySymbol(null) throws an exception.. 回答1: The following works. It's a bit ugly, but it fulfils the contract: NumberFormat nf = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols

How to read in numbers with a comma as decimal separator?

最后都变了- 提交于 2019-11-26 08:56:52
问题 I have a series of CSV files where numbers are formatted in the european style using commas instead of decimal points, i.e. 0,5 instead of 0.5 . There are too many of these files to edit them before importing to R. I was hoping there is an easy parameter for the read.csv() function, or a method to apply to the extracted dataset in order for R to treat the data as a number rather than a string. 回答1: When you check ?read.table you will probably find all the answer that you need. There are two

How do format a phone number as a String in Java?

限于喜欢 提交于 2019-11-26 08:56:49
问题 I have been storing phone numbers as longs and I would like to simply add hyphens when printing the phone number as a string. I tried using DecimalFormat but that doesn\'t like the hyphen. Probably because it is meant for formatting decimal numbers and not longs. long phoneFmt = 123456789L; DecimalFormat phoneFmt = new DecimalFormat(\"###-###-####\"); System.out.println(phoneFmt.format(phoneNum)); //doesn\'t work as I had hoped Ideally, I would like to have parenthesis on the area code too.