string.format

Double string.format

社会主义新天地 提交于 2019-11-30 21:43:40
问题 I have some double values I want to convert to a string with this pattern: 0.xx or x.xx Currently I have try this: double.ToString("#.#0"); What should I add in order to see zero in case my number start with zero? 回答1: just use myDouble.ToString("0.00") that should do the trick 回答2: myDouble.ToString("N2") should also work. Have a look at MSDN: Custom Numeric Format Strings MSDN: Standard Numeric Format Strings 回答3: String.Format("{0:0.00}", 111.0); 回答4: Put a zero in front of the decimal

Need a custom currency format to use with String.Format

亡梦爱人 提交于 2019-11-30 17:37:19
I'm trying to use String.Format("{0:c}", somevalue) in C# but am having a hard time figuring out how to configure the output to meet my needs. Here are my needs: 0 outputs to blank 1.00 outputs to $1.00 10.00 outputs to $10.00 100.00 outputs to $100.00 1000.00 outputs to $1,000.00 I've tried String.Format("{0:c}", somevalue) but for zero values it outputs $0.00 which is not what I want. I've also tried String.Format("{0:$0,0.00;$(0,0.00);#}", somevalue), but for 1.0 it outputs $01.00. String.Format("{0:$0.00;$(0.00);#}", somevalue) works for most cases, but when somevalue is 1000.00 the output

Format string with dashes

自作多情 提交于 2019-11-30 16:53:00
问题 I have a compressed string value I'm extracting from an import file. I need to format this into a parcel number, which is formatted as follows: ##-##-##-###-### . So therefore, the string "410151000640" should become "41-01-51-000-640". I can do this with the following code: String.Format("{0:##-##-##-###-###}", Convert.ToInt64("410151000640")); However, The string may not be all numbers; it could have a letter or two in there, and thus the conversion to the int will fail. Is there a way to

Can I format NULL values in string.Format?

那年仲夏 提交于 2019-11-30 10:44:26
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. My example is actually incorrect because I mistakenly thought Excel used the 3rd parameter if the value

Python string.format() percentage to one decimal place

流过昼夜 提交于 2019-11-30 08:06:27
In the example below I would like to format to 1 decimal place but python seems to like rounding up the number, is there a way to make it not round the number up? >>> '{:.1%}'.format(0.9995) '100.0%' >>> '{:.2%}'.format(0.9995) '99.95%' Thanks! :) If you want to round down always (instead of rounding to the nearest precision), then do so, explicitly, with the math.floor() function : from math import floor def floored_percentage(val, digits): val *= 10 ** (digits + 2) return '{1:.{0}f}%'.format(digits, floor(val) / 10 ** digits) print floored_percentage(0.995, 1) Demo: >>> from math import

String.Format(“{0:C2}”, -1234) (Currency format) treats negative numbers as positive

心不动则不痛 提交于 2019-11-30 06:36:09
I am using String.Format("{0:C2}", -1234) to format numbers. It always formats the amount to a positive number, while I want it to become $ - 1234 Am I right in saying it's putting it in brackets, i.e. it's formatting it as ($1,234.00) ? If so, I believe that's the intended behaviour for the US. However, you can create your own NumberFormatInfo which doesn't behave this way. Take an existing NumberFormatInfo which is "mostly right", call Clone() to make a mutable copy, and then set the CurrencyNegativePattern appropriately (I think you want value 2). For example: using System; using System

Need a custom currency format to use with String.Format

我们两清 提交于 2019-11-30 01:30:24
问题 I'm trying to use String.Format("{0:c}", somevalue) in C# but am having a hard time figuring out how to configure the output to meet my needs. Here are my needs: 0 outputs to blank 1.00 outputs to $1.00 10.00 outputs to $10.00 100.00 outputs to $100.00 1000.00 outputs to $1,000.00 I've tried String.Format("{0:c}", somevalue) but for zero values it outputs $0.00 which is not what I want. I've also tried String.Format("{0:$0,0.00;$(0,0.00);#}", somevalue), but for 1.0 it outputs $01.00. String

Safer compile-time String.format() alternative issue 2

倖福魔咒の 提交于 2019-11-29 12:36:57
With String.format , there seems to be a large opening for programmatic error that isn't found at compile-time. This can make fixing errors more complex and / or take longer. This was the issue for me that I set out to fix (or hack a solution). I came close, but I am not close enough. For this problem, this is more certainly over-engineered. I understand that, but I just want to find a good compile-time solution to this. More information can be found here. My question dealing with Basic Program 2 calculators. 1 Logic backend. Could be expanded to include other simple calculators. I wanted 1

How to use string.Format() to format a hex number surrounded by curly brackets?

筅森魡賤 提交于 2019-11-29 09:23:10
Input: uint hex = 0xdeadbeef; Required output: string result = "{deadbeef}" First approach: Explicitly add the { and } ; this works: result = "{" + string.Format("{0:x}", hex) + "}"; // -> "{deadbeef}" Output as decimal rather than hex using escaped curly brackets: result = string.Format("{{{0}}}", hex); // -> "{3735928559}" Seems promising, now all we need to do is add the :x hex specifer as per the first approach above: result = string.Format("{{{0:x}}}", hex); // -> "{x}" Oh dear, adding the ':x has made it output "{x}" rather than the "{deadbeef}" that I wanted. So my question is: Must I

java equivalent to printf(“%*.*f”)

为君一笑 提交于 2019-11-29 09:11:27
In C, the printf() statement allows the precision lengths to be supplied in the parameter list. printf("%*.*f", 7, 3, floatValue); where the asterisks are replaced with first and second values, respectively. I am looking for an equivalent in Android/Java; String.format() throws an exception. EDIT: Thanks, @Tenner; it indeed works. I use int places = 7; int decimals = 3; String.format("%" + places + "." + decimals + "f", floatValue); A little ugly (and string concatenation makes it not perform well), but it works. System.out.print(String.format("%.1f",floatValue)); This prints the floatValue