number-formatting

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

对着背影说爱祢 提交于 2019-11-26 18:53:15
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. new DecimalFormat("(###)-###-####"); What is the correct way to do this? You can use String.replaceFirst

Show only two digit after decimal [duplicate]

我的未来我决定 提交于 2019-11-26 18:42:51
This question already has an answer here: how to convert double to 2 number after the dot? [duplicate] 6 answers How to get the double value that is only two digit after decimal point. for example if i=348842. double i2=i/60000; tv.setText(String.valueOf(i2)); this code generating 5.81403333 . But I want only 5.81 . So what shoud I do? Use DecimalFormat . DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.

number_format() causes error “A non well formed numeric value encountered”

拈花ヽ惹草 提交于 2019-11-26 18:33:57
问题 I am using number_format to round floats to only 2 decimal digits. The problem is that some of my inputs don't have more than 2 decimals digits to begin with. So the code: number_format($value, 2) Instead of peacefully adding 0 in case it doesn't have enough decimal digits, it raises errors inside Apache log and that's not desirable. So number_format(2.1, 2) or number_format(0, 2) will raise error in Apache log. [Thu Jun 30 17:18:04 2011] [error] [client 127.0.0.1] PHP Notice: A non well

How to display Currency in Indian Numbering Format in PHP

微笑、不失礼 提交于 2019-11-26 18:09:48
问题 I have a question about formatting the Rupee currency (Indian Rupee - INR). For example, numbers here are represented as: 1 10 100 1,000 10,000 1,00,000 10,00,000 1,00,00,000 10,00,00,000 Refer Indian Numbering System I have to do with it PHP. I have saw this question Displaying Currency in Indian Numbering Format. But couldn't able to get it for PHP my problem. Update: How to use money_format() in indian currency format? 回答1: You have so many options but money_format can do the trick for you

How do you set the cout locale to insert commas as thousands separators?

元气小坏坏 提交于 2019-11-26 17:48:46
问题 Given the following code: cout << 1000; I would like the following output: 1,000 This can be done using std::locale, and the cout.imbue() function, but I fear I may be missing a step here. Can you spot it? I'm currently copying the current locale, and adding a thousands separator facet, but the comma never appears in my output. template<typename T> class ThousandsSeparator : public numpunct<T> { public: ThousandsSeparator(T Separator) : m_Separator(Separator) {} protected: T do_thousands_sep(

Write a number with two decimal places SQL server

泄露秘密 提交于 2019-11-26 17:32:32
How do you write a number with two decimal places for sql server? try this SELECT CONVERT(DECIMAL(10,2),YOURCOLUMN) Use Str() Function. It takes three arguments(the number, the number total characters to display, and the number of decimal places to display Select Str(12345.6789, 12, 3) displays: ' 12345.679' ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate, (unless the integer part is too large for the total size, in which case asterisks are displayed instead.) for a Total of 12 characters, with 3 to the right of decimal point. AAA

PHP number format

廉价感情. 提交于 2019-11-26 17:29:21
问题 I have this string : 000000000000100 and need to convert it to: 1,00 So, the rules are: Divide the number by 100 and use a comma as decimal separator Strip leading zeros Keep two decimals 回答1: From the PHP Manual page on number_format: string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' ) If you want numbers like 123456 be formatted as 1234,45 , use: echo number_format($number / 100, 2, ",", ""); If you need a dot as thousands

c++: Format number with commas?

回眸只為那壹抹淺笑 提交于 2019-11-26 17:24:13
I want to write a method that will take an integer and return a std::string of that integer formatted with commas. Example declaration: std::string FormatWithCommas(long value); Example usage: std::string result = FormatWithCommas(7800); std::string result2 = FormatWithCommas(5100100); std::string result3 = FormatWithCommas(201234567890); // result = "7,800" // result2 = "5,100,100" // result3 = "201,234,567,890" What is the C++ way of formatting a number as a string with commas? (Bonus would be to handle double s as well.) Use std::locale with std::stringstream #include <iomanip> #include

Add 'decimal-mark' thousands separators to a number

拥有回忆 提交于 2019-11-26 16:33:51
How do I format 1000000 to 1.000.000 in Python? where the '.' is the decimal-mark thousands separator. 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 higher. 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 will use your locale's thousands separator, e.g. >>> import locale >>> locale.setlocale(locale.LC_ALL, 'de

Problem parsing currency text to decimal type

混江龙づ霸主 提交于 2019-11-26 15:27:30
I am trying to parse a string like "$45.59" into a decimal. For some reason I am getting exception that the input was not in the correct format. I don't care about all the localization stuff because this is not going to be a global program. Here is what I am doing. Do you see any problems? NumberFormatInfo MyNFI = new NumberFormatInfo(); MyNFI.NegativeSign = "-"; MyNFI.NumberDecimalSeparator = "."; MyNFI.NumberGroupSeparator = ","; MyNFI.CurrencySymbol = "$"; decimal d = decimal.Parse("$45.00", MyNFI); // throws exception here... How about using: decimal d = decimal.Parse("$45.00",