string-formatting

Include a variable inside a NSString?

怎甘沉沦 提交于 2019-11-30 17:40:47
问题 This works fine, we all know that: NSString *textoutput = @"Hello"; outLabel.text = textoutput; However, what if you want to include a variable inside that NSString statement like the following: NSString *textoutput =@"Hello" Variable; In C++ I know when I cout something and I wanted to include a variable all I did was soemthing like this: cout << "Hello" << variableName << endl; So I'm trying to accomplish that with Objective-C but I don't see how. 回答1: You can do some fancy formatting using

Text formatting error: '=' alignment not allowed in string format specifier

孤人 提交于 2019-11-30 17:02:52
What does '=' alignment mean in the following error message, and why does this code cause it? >>> "{num:03}".format(num="1") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: '=' alignment not allowed in string format specifier The code has a subtle problem: the input value "1" is text, not a number. But the error message doesn't appear to have anything to do with that. Nothing in the error message indicates why “'=' alignment” is relevant, and it does not appear in the code. So what is the significance of emitting that error message? The error message occurs

Formatting dict keys: AttributeError: 'dict' object has no attribute 'keys()'

只谈情不闲聊 提交于 2019-11-30 15:11:42
What is the proper way to format dict keys in string? When I do this: >>> foo = {'one key': 'one value', 'second key': 'second value'} >>> "In the middle of a string: {foo.keys()}".format(**locals()) What I expect: "In the middle of a string: ['one key', 'second key']" What I get: Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> "In the middle of a string: {foo.keys()}".format(**locals()) AttributeError: 'dict' object has no attribute 'keys()' But as you can see, my dict has keys: >>> foo.keys() ['second key', 'one key'] You can't call methods in the placeholders. You

Linq-to-Entities: Format Date in select query expression

吃可爱长大的小学妹 提交于 2019-11-30 14:57:40
I am trying to get a formatted date string directly from a LINQ-to-Entities query expression. nonBusinessDays = (from ac in db.AdminCalendar where ac.DateTimeValue >= calendarStartDate && ac.DateTimeValue <= calendarEndDate && ac.IsBusinessDay == false select ac.MonthValue + "/" + ac.DayOfMonth + "/" + ac.FullYear).ToList(); But, I get the folloinw error message: "Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types." Is there any way to do this besides iterating through the result set? Thanks! Abe I found

In PHP, how do I add to a zero-padded numeric string and preserve the zero padding?

孤人 提交于 2019-11-30 13:56:20
问题 If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002 . How do I solve this problem? 回答1: $foo = sprintf('%04d', $foo + 1); 回答2: It would probably help you to understand the PHP data types and how they're affected when you do operations to variables of various types. You say you have "a variable in PHP say 0001", but what type is that variable? Probably a string, "0001", since an integer can't have that value (it's just 1). So when you do this: echo (

NumberFormat text field without commas

不羁的心 提交于 2019-11-30 13:50:02
I have a JFormattedTextField which I want to accept numbers in the 5 digit range. The following code works correctly: myNumberBox = new JFormattedTextField(NumberFormat.getIntegerInstance()); However, when I type "12345" into the field and switch focus, a comma is inserted due to my locale, making the text "12,345". How can I prevent commas from being added to my input? Better yet, can they be stripped out even if the user does insert commas? You have to disable the grouping in your NumberFormat object like this: NumberFormat format = NumberFormat.getIntegerInstance(); format.setGroupingUsed

How to calculate the number of significant decimal digits of a c++ double?

六眼飞鱼酱① 提交于 2019-11-30 13:46:28
When dealing with floating point values in Java, calling the toString() method gives a printed value that has the correct number of floating point significant figures. However, in C++, printing a float via stringstream will round the value after 5 or less digits. Is there a way to "pretty print" a float in C++ to the (assumed) correct number of significant figures? EDIT: I think I am being misunderstood. I want the output to be of dynamic length, not a fixed precision. I am familiar with setprecision. If you look at the java source for Double, it calculates the number of significant digits

Get AM/PM for a date time in lowercase using only a datetime format

一世执手 提交于 2019-11-30 13:34:48
问题 I'm to get a custom DateTime format including the AM/PM designator, but I want the "AM" or "PM" to be lowercase without making the rest of of the characters lowercase. Is this possible using a single format and without using a regex? Here's what I've got right now: item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt") An example of the output right now would be Saturday, January 31, 2009 at 1:34PM 回答1: I would personally format it in two parts: the non-am/pm part, and the am/pm part with

Convert yyyy-MM-ddTHH:mm:ss.fffZ to DateTime in JavaScript manually

核能气质少年 提交于 2019-11-30 13:15:02
I receive from a Webservice a String with a date in this format: yyyy-MM-ddTHH:mm:ss.fffZ I need to convert that String with JavaScript to a normal DateTime but without using the new Date('yyyy-MM-ddTHH:mm:ss.fffZ') because I'm using an old version of JavaScript that not support that conversion. I can split that string and get the: Year Month Days Time but how to manipulate the time zone "fffZ" Any suggestions? Here's a one liner from John Resig : var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), I've founded the solution. Please check http://webcloud.se/log/JavaScript

WPF TextBox Binding with Formatting

天大地大妈咪最大 提交于 2019-11-30 12:36:57
I have just upgraded our wpf application from 3.5sp1 to 4.0. The code below we use to bind the textbox to the underlying view model. The textbox is editable. <TextBox HorizontalContentAlignment="Right" Text="{Binding Path=Price, StringFormat={0:#,##0;(#,##0)}, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/> In 3.5sp1 the formatting would only occur initially. So when the textbox was loaded and bound to value 4000, the formatting would change it to 4,000. If user edited this value no formatting would occur. In 4.0 the formatting