string-formatting

How should I properly use __attribute__ ((format (printf, x, y))) inside a class method in C++?

廉价感情. 提交于 2019-12-18 14:01:57
问题 I'm trying to define a class method for debug prints that will behave like printf : inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))) This complains about: error: format string argument not a string type I recalled that a class method declaration has an implicit this parameter, so I changed the locations of the parameters to 2, 3: inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 2, 3))) and now it compiles, but it looks like the parameters

Can I format NULL values in string.Format?

人盡茶涼 提交于 2019-12-18 13:54:10
问题 I was wondering if there's a syntax for formatting NULL values in string.Format, such as what Excel uses For example, using Excel I could specify a format value of {0:#,000.00;-#,000.00,NULL} , which means display the numeric value as number format if positive, number format in parenthesis if negative, or NULL if the value is null string.Format("${0:#,000.00;(#,000.00);NULL}", someNumericValue); Edit I'm looking for formatting NULL / Nothing values for all data types, not just numeric ones.

In C#, what is the best method to format a string as XML?

允我心安 提交于 2019-12-18 13:54:08
问题 I am creating a lightweight editor in C# and would like to know the best method for converting a string into a nicely formatted XML string. I would hope that there's a public method in the C# library like "public bool FormatAsXml(string text, out string formattedXmlText)", but it couldn't be that easy, could it? Very specifically, what would the method "SomeMethod" have to be that would produce the output below? string unformattedXml; string formattedXml; unformattedXml = "<?xml version=\"1.0

Why use endl when I can use a newline character? [duplicate]

懵懂的女人 提交于 2019-12-18 09:59:45
问题 This question already has answers here : C++: “std::endl” vs “\n” (13 answers) Closed 2 years ago . Is there a reason to use endl with cout when I can just use \n ? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl , or am I missing something? 回答1: endl appends '\n' to the stream and calls flush() on the stream. So cout << x << endl; is equivalent to cout << x << '\n'; cout.flush(); A stream may use an internal buffer which gets actually streamed when

What does bb or bbbb represent in an EN-US TEXT function or Custom Number format mask?

倖福魔咒の 提交于 2019-12-18 09:26:22
问题 On a EN-US regional system, what does B or b represent in a format mask? While it does nothing in VBA's Format function, both the TEXT function and Custom Number Formatting recognize b as a legal Number Format Code and return a number that I cannot determine the origin of. To further confuse matters, larger numbers return different values when formatted with bbbb and while blocks of sequential numbers return the same value, there are transitions when they jump ahead a digit. I do know that

Wpf Binding Stringformat to show only first character

纵然是瞬间 提交于 2019-12-18 07:06:46
问题 Is there any way so that i can show only first character of a Bound string on a textblock..? For eg;If i Bind 'Male', my textblock should only show 'M'..... 回答1: You might use a value converter to return a string prefix: class PrefixValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string s = value.ToString(); int prefixLength; if (!int.TryParse(parameter.ToString(), out prefixLength) || s

Formatting Floating Point Numbers

流过昼夜 提交于 2019-12-18 04:38:16
问题 I have a variable of type double , I need to print it in upto 3 decimals of precision but it shouldn't have any trailing zeros... eg. I need 2.5 // not 2.500 2 // not 2.000 1.375 // exactly till 3 decimals 2.12 // not 2.120 I tried using DecimalFormatter , Am i doing it wrong? DecimalFormat myFormatter = new DecimalFormat("0.000"); myFormatter.setDecimalSeparatorAlwaysShown(false); Thanks. :) 回答1: Try the pattern "0.###" instead of "0.000" : import java.text.DecimalFormat; public class Main {

Python string format: When to use !s conversion flag

不羁的心 提交于 2019-12-18 04:36:05
问题 What's the difference between these 2 string format statements in Python: '{0}'.format(a) '{0!s}'.format(a) Both have the same output if a is an integer, list or dictionary. Is the first one {0} doing an implicit str() call? Source PS: keywords: exclamation / bang "!s" formatting 回答1: It is mentioned in the documentation: The conversion field causes a type coercion before formatting. Normally, the job of formatting a value is done by the __format__() method of the value itself. However, in

Convert, or unformat, a string to variables (like format(), but in reverse) in Python

六月ゝ 毕业季﹏ 提交于 2019-12-18 01:59:42
问题 I have strings of the form Version 1.4.0\n and Version 1.15.6\n , and I'd like a simple way of extracting the three numbers from them. I know I can put variables into a string with the format method; I basically want to do that backwards, like this: # So I know I can do this: x, y, z = 1, 4, 0 print 'Version {0}.{1}.{2}\n'.format(x,y,z) # Output is 'Version 1.4.0\n' # But I'd like to be able to reverse it: mystr='Version 1.15.6\n' a, b, c = mystr.unformat('Version {0}.{1}.{2}\n') # And have

Fixed width number formatting python 3

为君一笑 提交于 2019-12-18 01:18:08
问题 How do I get an integer to fill 0's to a fixed width in python 3.2 using the format attribute? Example: a = 1 print('{0:3}'.format(a)} gives ' 1' instead of '001' I want. In python 2.x, I know that this can be done using print "%03d" % number. I checked the python 3 string documentation but wasn't able to get this. http://docs.python.org/release/3.2/library/string.html#format-specification-mini-language Thanks. 回答1: Prefix the width with a 0 : >>> '{0:03}'.format(1) '001' Also, you don't need