string-formatting

Convert int to shortened, formatted string

故事扮演 提交于 2020-01-09 07:38:24
问题 The user will enter a dollar value as an int , and I'd like to convert the result into a shortened, formatted string. So if the user enters 1700, the string would say "$1.7k". If the user enters 32600000, the string would say "$32.6m". Update Here's the code I have so far. It seems to be working for numbers ~10k. I would just add more if statements for bigger numbers. But is there a more efficient way to do this? NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; [nformat

print variable-name in Matlab

天涯浪子 提交于 2020-01-09 03:45:07
问题 I have a function in Matlab that has some variables in it. I need to print out the variable_names (in case of an exception etc.). I am aware of inputname function but it works for input_arguments only. mat = [ 1 2 ; 3 4 ] ; % disp(['Error in var: ' ??(a)]) % desired ouput: Error in var: mat (and NOT 1 2 ; 3 4!) Thanks! 回答1: Matlab essentially does not let you do that. However, you can write a helper function to ease your pain in creating output like that: function disp_msg_var(msg, v) disp(

String.Format and SqlParameters

折月煮酒 提交于 2020-01-07 05:25:09
问题 I've been looking through the documentation on string formatting in .Net and haven't found this bit yet, and was hoping someone could point me in the right direction. I've come across a working piece of code that takes SqlParameters and puts them in a string like this: SqlParameter[] arrParams = new SqlParameter[] { new SqlParameter("@ClientID", clid), new SqlParameter("@CustomerID", cuid), new SqlParameter("@AdminUser", false) }; string sqlText = string.Format("Insert into [Table1] (RID,

python string format with both list and string

核能气质少年 提交于 2020-01-06 20:25:40
问题 I want to use string formatting to insert variable values into mystring where some of the variables are normal values and some are list values. myname = 'tom' mykids = ['aa', 'bb', 'cc'] mystring = """ hello my name is %s and this are my kids %s, %s, %s """ % (myname, tuple(mykids)) I get the error not enough arguments because i probably did the tuple(mykids) wrong. help appreciated. 回答1: You can use str.format() instead: >>> myname = 'tom' >>> mykids = ['aa','bb','cc'] >>> mystring = 'hello

python string format with both list and string

☆樱花仙子☆ 提交于 2020-01-06 20:25:22
问题 I want to use string formatting to insert variable values into mystring where some of the variables are normal values and some are list values. myname = 'tom' mykids = ['aa', 'bb', 'cc'] mystring = """ hello my name is %s and this are my kids %s, %s, %s """ % (myname, tuple(mykids)) I get the error not enough arguments because i probably did the tuple(mykids) wrong. help appreciated. 回答1: You can use str.format() instead: >>> myname = 'tom' >>> mykids = ['aa','bb','cc'] >>> mystring = 'hello

Define Stringformat for tabs?

て烟熏妆下的殇ゞ 提交于 2020-01-06 19:44:13
问题 I have a little bug to fix. You can see in the Picture 3 Months outputed. So that is my code how I displat this result. I try String.format but I dont know how to use it at this tabs. I try it with this tabs but unfortunally I find out that the format for the first Month (January) is short then the other month. All months use 3 tabs but not the first one. public LinkedList<String> buildMonth(int month) { int lengthOfMonth = calender.getLengthOfMonth(this.year, month); LinkedList<String>

C# - How to Properly Indent String Data with Tabs?

纵然是瞬间 提交于 2020-01-06 03:49:06
问题 In my C# Console program I have 4 variables. Their names and types are as follows: int ID bool Status bool Available int Count I would like to be able to print them to the Console, nicely indented as follows: However, when I use the tabs "\t", to format my string, it does not take into account the text width, and all the values are indented waywardly as follows: How do I fix this? I don't want to use any third party libraries, but simply .NET functionality such as String.Format(). 回答1: You

binding string format from code-behind?

人盡茶涼 提交于 2020-01-04 12:48:33
问题 Please, can some body tell me how to get my double value formatted like "0.0" from code-behind, like this: Binding b = new Binding(DoubleValue); b.StringFormat = "????"; In xaml it works just like that "0.0"... 回答1: What about this? b.StringFormat = "{0:F1}"; See the documentation of StringFormat and also Standard Numeric Format Strings and Custom Numeric Format Strings. EDIT: Just to make clear how a binding would be created and assigned (to the Text property of an imaginary TextBlock named

Python 3.6+ formatting strings from unpacking dictionaries with missing keys

若如初见. 提交于 2020-01-04 12:43:21
问题 In Python3.4 you could do the following thing: class MyDict(dict): def __missing__(self, key): return "{%s}" % key And then something like: d = MyDict() d['first_name'] = 'Richard' print('I am {first_name} {last_name}'.format(**d)) Printing, as expected: I am Richard {last_name} But this snippet won't work in Python3.6+, returning a KeyError while trying to get the last_name value from the dictionary, is there any workaround for that string formatting to work in the same way as in Python3.4?

Python 3.6+ formatting strings from unpacking dictionaries with missing keys

淺唱寂寞╮ 提交于 2020-01-04 12:42:05
问题 In Python3.4 you could do the following thing: class MyDict(dict): def __missing__(self, key): return "{%s}" % key And then something like: d = MyDict() d['first_name'] = 'Richard' print('I am {first_name} {last_name}'.format(**d)) Printing, as expected: I am Richard {last_name} But this snippet won't work in Python3.6+, returning a KeyError while trying to get the last_name value from the dictionary, is there any workaround for that string formatting to work in the same way as in Python3.4?