string-formatting

C++ printf with %f but localized for the user's country

白昼怎懂夜的黑 提交于 2019-11-29 03:25:29
I'm using the following C++ syntax to output a floating point value on a Windows platform: printf("%.2f", 1.5); It works well if I run it on an English user account. My assumption was that if I run it on, say French user account, the output will be 1,50 instead of 1.50. Why do I not see it and how to produce my desired result? The radix character (i.e. '.' or ',') is defined by the current locale. The default locale (at least for Windows systems) is "C", which defines '.' as radix character. You can set the current locale for a C/C++ program using the setlocale function. To set the locale to

How to format a number as percentage without the percentage sign?

匆匆过客 提交于 2019-11-29 03:01:53
How do I in .NET format a number as percentage without showing the percentage sign? If I have the number 0.13 and use the format string {0:P0} the output is 13 % . However I would like to get 13 instead, without having to multiply the number by 100 and using the format string {0:N0} . (Background: In ASP.NET I have a GridView with a BoundField where I would like to display a number as percentage but without the percentage sign (%). How do I do that?) Thanks for the answers. At the time of editing 4 out of 6 suggest what I would like to avoid, as explained above. I was looking for a way to use

Using locals() and format() method for strings: are there any caveats?

余生长醉 提交于 2019-11-29 02:04:29
问题 Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = 'john', age=22): userId = user.getUserId() return "Name: {name}, age: {age}, userid:{userId}".format(**locals()) I had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: "

How do I add slashes to a string in Javascript?

大城市里の小女人 提交于 2019-11-29 01:32:01
问题 Just a string. Add \' to it every time there is a single quote. 回答1: replace works for the first quote, so you need a tiny regular expression: str = str.replace(/'/g, "\\'"); 回答2: Following JavaScript function handles ', ", \b, \t, \n, \f or \r equivalent of php function addslashes(). function addslashes(string) { return string.replace(/\\/g, '\\\\'). replace(/\u0008/g, '\\b'). replace(/\t/g, '\\t'). replace(/\n/g, '\\n'). replace(/\f/g, '\\f'). replace(/\r/g, '\\r'). replace(/'/g, '\\\'').

Format EditText view for phone numbers

北战南征 提交于 2019-11-29 01:30:48
问题 I have an EditText view and I want it to format the user's input into the phone number format. For example, when the user types in 1234567890, the EditText view should dynamically show it as "(123) 456-7890" as soon as the first 3 numbers are inputted. I tried the following in my OnCreate but it didn't seem to do anything for me... EditText ET = (EditText) findViewById(R.id.add_number); ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); How can I get the user's input to

Python: Logging TypeError: not all arguments converted during string formatting

被刻印的时光 ゝ 提交于 2019-11-29 01:03:01
Here is what I am doing >>> import logging >>> logging.getLogger().setLevel(logging.INFO) >>> from datetime import date >>> date = date.today() >>> logging.info('date={}', date) Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 846, in emit msg = self.format(record) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 723, in format return fmt.format(record) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/_

How to produce “human readable” strings to represent a TimeSpan

本秂侑毒 提交于 2019-11-28 23:17:49
I have a TimeSpan representing the amount of time a client has been connected to my server. I want to display that TimeSpan to the user. But I don't want to be overly verbose to displaying that information (ex: 2hr 3min 32.2345sec = too detailed!) For example: If the connection time is... > 0 seconds and < 1 minute -----> 0 Seconds > 1 minute and < 1 hour -----> 0 Minutes, 0 Seconds > 1 hour and < 1 day -----> 0 Hours, 0 Minutes > 1 day -----> 0 Days, 0 Hours And of course, in cases where the numeral is 1 (ex: 1 seconds, 1 minutes, 1 hours, 1 days), I would like to make the text singular (ex:

Format decimal value to string with leading spaces

谁说胖子不能爱 提交于 2019-11-28 22:17:51
How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100? For example, a decimal value of 12.3456 should be output as " 12.3" with single leading space. 10.011 would be " 10.0" . 123.123 is "123.1" I'm looking for a solution, that works with standard/custom string formatting, i.e. decimal value = 12.345456; Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern. This pattern {0,5:###.0} should work: string.Format("{0,5:###.0}", 12.3456) //Output " 12.3" string.Format("{0,5:###.0}", 10.011) //Output " 10.0"

How to deal with “%1” in the argument of QString::arg()?

天大地大妈咪最大 提交于 2019-11-28 21:18:49
Everybody loves QString("Put something here %1 and here %2") .arg(replacement1) .arg(replacement2); but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second QString::arg() will replace only the re-introduced %1 or both %2 occurrences. Anyway, you won't get the literal "%1" that you probably intended. Is there any standard trick to overcome this? If you need an example to play with, take this #include <QCoreApplication> #include <QDebug> int main() { qDebug() << QString("%1-%2").arg("%1").arg("foo"); return 0; }

python centre string using format specifier

无人久伴 提交于 2019-11-28 21:15:13
I have a string called Message. Message = "Hello, welcome!\nThis is some text that should be centered!" Yeah, it's just a test statement... And I'm trying to centre it for a default Terminal window, i.e. of 80 width, with this statement: print('{:^80}'.format(Message)) Which prints: Hello, welcome! This is some text that should be centered! I'm expecting something like: Hello, welcome! This is some text that should be centered! Any suggestions? You need to centre each line separately: '\n'.join('{:^80}'.format(s) for s in Message.split('\n')) Here is an alternative that will auto center your