string-formatting

C# format as currency with no trailing zero decimal numbers, consider negative, and different culture

☆樱花仙子☆ 提交于 2019-12-04 18:31:37
How can we format a number to currency without trailing zero decimal numbers? Basically it should behave as the "C" format specifier but without trailing zeroes. Below are the test cases. value | en-US | fr-FR 1 | $1 | 1 € 1.0 | $1 | 1 € 1.1 | $1.1 | 1,1 € 1.10 | $1.1 | 1,1 € -1 | ($1) | -1 € -1.0 | ($1) | -1 € -1.1 | ($1.1) | -1,1 € 1000 | $1,000 | 1 000 € 1000.0 | $1,000 | 1 000 € Is there a way to achieve this behavior by leveraging the "C" format specifier? side note: I am continuing from this related wpf question but focusing on the formatting part and with more exhaustive test cases. I

Format string with trailing zeros removed for x decimal places in Swift [duplicate]

怎甘沉沦 提交于 2019-12-04 17:48:24
问题 This question already has answers here : Formatting decimal places with unknown number (4 answers) Closed 4 years ago . This in Swift (1.2) let doubleValue1 = Double(10.116983123) println(String(format: "%.2f", doubleValue1)) let doubleValue2 = Double(10.0) println(String(format: "%.2f", doubleValue2)) Prints 10.12 10.00 I'm looking for a way using a formatter or a direct string format and not via string manipulation, to remove the trailing zeroes, so to print: 10.12 10 The closest I got is:

Is there a way to build a Java String using an SLF4J-style formatting function?

主宰稳场 提交于 2019-12-04 16:26:52
问题 I've heard that using StringBuilder is faster than using string concatenation, but I'm tired of wrestling with StringBuilder objects all of the time. I was recently exposed to the SLF4J logging library and I love the "just do the right thing" simplicity of its formatting when compared with String.format. Is there a library out there that would allow me to write something like: int myInteger = 42; MyObject myObject = new MyObject(); // Overrides toString() String result = CoolFormatingLibrary

Use str.format() to access object attributes

半城伤御伤魂 提交于 2019-12-04 16:09:16
问题 I have a Python object with attributes a , b , c . I still use old string formatting, so I'd normally print these manually: print 'My object has strings a=%s, b=%s, c=%s' % (obj.a, obj.b, obj.c) Lately, my strings have been getting super long, and I'd much rather be able to simply pass the object into a string format function, something like: print 'My object has strings a=%a, b=%b, c=%c'.format(obj) However, the syntax is incorrect. Is this possible? 回答1: You can use the .attribute_name

PHP remove first zeros

房东的猫 提交于 2019-12-04 16:05:44
问题 Want to remove all 0 placed at the beginning of some variable. Some options: if $var = 0002 , we should strip first 000 ( $var = 2 ) if var = 0203410 we should remove first 0 ( $var = 203410 ) if var = 20000 - do nothing ( $var = 20000 ) What is the solution? 回答1: cast it to integer $var = (int)$var; 回答2: Maybe ltrim ? $var = ltrim($var, '0'); 回答3: $var = ltrim($var, '0'); This only works on strings, numbers starting with a 0 will be interpreted as octal numbers, multiple zero's are ignored.

Formatting a string with string.Format(“{0:00}”

帅比萌擦擦* 提交于 2019-12-04 15:33:32
问题 I have just taken over some code and I see this used a lot. It seems to take the integer and create a string looking like "01", "02" etc. What I am not sure of is the convention used here. Why is the format {0:00} and not {00} ? string.Format("{0:00}", int.Parse(testVal) + 1); 回答1: The first 0 is the placeholder, means the first parameter. 00 is an actual format. For example it could be like this: var result = string.Format("{0:00} - {1:00}", 5, 6); result will be 05 - 06 . So the first 0 is

Problem with UpdateSourceTrigger=PropertyChanged and StringFormat in WPF

大兔子大兔子 提交于 2019-12-04 14:23:01
问题 I have a text box in my application which is data bound to a decimal field in my class and the binding mode is two way. I am using StringFormat={0:c} for currency formatting. This works fine as long as I don't touch 'UpdateSourceTrigger'. If I set UpdateSourceTrigger=PropertyChanged , It stops formatting the text that I am entering. here is my code example Employee.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace

Capturing **vars() pattern in string formatting

主宰稳场 提交于 2019-12-04 13:13:10
I frequently find myself using the following pattern for string formatting. a = 3 b = 'foo' c = dict(mykey='myval') #prints a is 3, b is foo, mykey is myval print('a is {a}, b is {b}, mykey is {c[mykey]}'.format(**vars())) That is, I often have the values I need to print in the local namespace, represented by a call to vars(). As I look over my code, however, it seems awfully unpythonic to be constantly repeating the .format(**vars()) pattern. I'd like to create a function that will capture this pattern. It would be something like the following. # doesn't work def lfmt(s): """ lfmt (local

Any pretty python string formatting for a counter?

喜夏-厌秋 提交于 2019-12-04 12:41:06
Is there any string formatting for using correct suffix with log messages, for example: for n in itertools.count(): print 'printing for the {:nth} time'.format(n) Expected output: printing for the 0th time printing for the 1st time printing for the 2nd time printing for the 3rd time printing for the 4th time printing for the 5th time ... printing for the 23rd time ... printing for the 42nd time ... etc I could roll my own fairly easily, but I was wondering if there was already a built-in solution. If not, I will accept as answer the most elegant solution! What about simply: def stringify(x):

printf a float value with precision (number of decimal digits) passed in a variable [duplicate]

浪尽此生 提交于 2019-12-04 11:47:59
This question already has an answer here: Printf variable number of decimals in float 2 answers I can print a float value with precision of 3 decimal digits with double f = 1.23456; printf( "%.3f", f ); Now I have the number of requested decimal digits that is not fixed but stored into a variable int precision; precision = atoi( argv[ 1 ] ); // ...just for example double f = 1.23456; How do I print the value of f with the number of decimal digits specified in precision ? I can programmatically compose the format string for printf but I'm wondering if there is a more simple way to do it. Use ".