string-formatting

Python: Writing multiple nested dictionaries in one table in a text file separated by tab

天涯浪子 提交于 2020-01-11 11:46:08
问题 I am new to Python and I am struggling with printing multiple nested dictionaries in one table separated by tab. I wrote a script to read data from an input file and store the data in 3 different dictionaries: d1 = {'Ben': {'Skill': 'true', 'Magic': 'false'}, 'Tom': {'Skill': 'true', 'Magic': 'true'}} d2 = {'Ben': {'Strength': 'wo_mana', 'Int': 'wi_mana', 'Speed': 'wo_mana'}, 'Tom': {'Int': 'wi_mana', 'Agility': 'wo_mana'}} d3 = {'Ben': {'Strength': '1.10', 'Int': '1.20', 'Speed': '1.50'},

Delphi TFloatField.DisplayFormat for numeric fields less than 1.0

大城市里の小女人 提交于 2020-01-11 06:13:29
问题 This is my procedure. procedure format_integer_field(Atable: TDataSet); var i: integer; begin if Atable.Active then if Atable.FieldCount > 0 then with Atable do begin for i:= 0 to FieldCount-1 do if (Fields[i] is TIntegerField) then begin (Fields[i] as TIntegerField).DisplayFormat := '###,###'; (Fields[i] as TIntegerField).EditFormat := '#'; end else if (Fields[i] is TFloatField) then begin (Fields[i] as TFloatField).DisplayFormat := '###,###.##'; (Fields[i] as TFloatField).EditFormat := '#.#

WPF Binding and Dynamically Assigning StringFormat Property

拟墨画扇 提交于 2020-01-11 04:39:30
问题 I have a form that is generated based on several DataTemplate elements. One of the DataTemplate elements creates a TextBox out of a class that looks like this: public class MyTextBoxClass { public object Value { get;set;} //other properties left out for brevity's sake public string FormatString { get;set;} } I need a way to "bind" the value in the FormatString property to the "StringFormat" property of the binding. So far I have: <DataTemplate DataType="{x:Type vm:MyTextBoxClass}"> <TextBox

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

与世无争的帅哥 提交于 2020-01-10 19:08:34
问题 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

WPF TextBox Binding with Formatting

佐手、 提交于 2020-01-10 18:52:20
问题 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

python centre string using format specifier

孤街醉人 提交于 2020-01-09 19:05:19
问题 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? 回答1: You need to centre each line separately: '

python centre string using format specifier

試著忘記壹切 提交于 2020-01-09 19:04:46
问题 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? 回答1: You need to centre each line separately: '

python centre string using format specifier

柔情痞子 提交于 2020-01-09 19:04:26
问题 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? 回答1: You need to centre each line separately: '

Format an Integer using Java String Format

与世无争的帅哥 提交于 2020-01-09 08:37:47
问题 I am wondering if it is possible, using the String.format method in Java, to give an integer preceding zeros? For example: 1 would become 001 2 would become 002 ... 11 would become 011 12 would become 012 ... 526 would remain as 526 ...etc At the moment I have tried the following code: String imageName = "_%3d" + "_%s"; for( int i = 0; i < 1000; i++ ){ System.out.println( String.format( imageName, i, "foo" ) ); } Unfortunately, this precedes the number with 3 empty spaces. Is it possible to

Convert int to shortened, formatted string

一笑奈何 提交于 2020-01-09 07:38:32
问题 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