string-formatting

Format numbers to strings in Python

I need to find out how to format numbers as strings. My code is here: return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of my string outputting "5:30:59.07 pm", it would displa...

Display number with leading zeros.

Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits. Essentially displaying 01 10 100 ...

Named string formatting in C#

Is there any way to format a string by name rather than position in C#? In python, I can do something like this example (shamelessly stolen from here): >>> print '%(language)s has %(#)03d quote types.' % \ {'language': "Python", "#": 2} Python has 002 quote types. Is there any way to do this in C#? Say for instance: String.Fo...

The simplest way of printing a portion of a char[] in C

Let's say I have a char* str = "0123456789" and I want to cut the first and the last three letters and print just the middle, what is the simplest, and safest, way of doing it? Now the trick: The portion to cut and the portion to print are of variable size, so I could have a very long char*, or a very small one. ...

How to use Single Quotes in Eval Format String

I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField. The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview. The SelectCommand has a WHERE clause which is to compare a string. Because I have already used the single and double quotes, I am having trouble delimiting ...

Output single character in C

When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"? ...

Custom numeric format string to always display the sign

Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it shoudl do for zero, I'm not sure!) ...

java decimal String format

I need to format a decimal value to a string where i always display at lease 2 decimals and at most 4. so for example "34.49596" would be "34.4959" "49.3" would be "49.30" can this be done using the String.format command? Or is there an easier/better way to do this in java. ...

Get AM/PM for a date time in lowercase using only a datetime format

I'm to get a custom DateTime format including the AM/PM designator, but I want the "AM" or "PM" to be lowercase without making the rest of of the characters lowercase. Is this possible using a single format and without using a regex? Here's what I've got right now: item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt") An example ...

Is there a format string that can be passed to the VB6 Format function that will simply group digits?

Is there a format string (?) I can pass to the VB6 Format function that will perform digit grouping? I want to replace the question mark in this statement... Debug.Print Format(123456789, "?") ...with a string that will generate the following output: 123,456,789 The predefined format string "Standard" comes close, but it tacks a d...

Return a tuple of arguments to be fed to string.format()

Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter and then pass them to the string method. My code looks something like this: class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = PairEvaluator() cards = pe.returnArbit...

Adding html in DataTextFormatString

I have a DropDownList populated from a LINQ query. As per the design requirements of this project, I need to append 4 " " to the DDL's ListItem.Text. It works when I add the ListItems manually like so: <asp:ListItem Value="NULL">&nsbp;&nsbp;&nsbp;&nsbp;Select One</asp:ListItem> but not when I DataBind() and use: DataTextFormatStrin...

How to convert between "#,##0" and "{0:n2}" style format string representations?

.NET supports two types of string formatting. I'm in a situation where existing configuration data has #,##0 style formatting. A new feature requires formatting to the same output, but the API needed for this feature only accepts formatting of type {0:n2}. Does anyone know of a means to convert between these two representations for nu...

Utility code to DataSet/DataReader table to console in Tabular format, table dimensions set at runtime.

Hello, I am essentially trying to replicate the same features as SQL Management Studio's Results To Text, to prettify my DataSet/DataReader object into a string based table. I am looking for a free (GNU or BSD/MIT licensed or equivalent) utility code if existing which will let me print out data in a tabular format. I am appending to ...

Accounting Style string format in ASP.NET

I would like to know the easiest way to format a string as accounting style. I know how to format as currency using {0:c} but there are some differences in accounting style, for example, all the dollar signs will line up as well as all the decimal points, and negatives are expressed in parenthesis rather than with a "-" minus sign. You...

In PHP, how do I add to a zero-padded numeric string and preserve the zero padding?

If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002. How do I solve this problem? ...

Best way to format integer as string with leading zeros?

I need to add leading zeros to integer to make a string with defined quantity of digits ($cnt). What the best way to translate this simple function from PHP to Python: function add_nulls($int, $cnt=2) { $int = intval($int); for($i=0; $i<($cnt-strlen($int)); $i++) $nulls .= '0'; return $nulls.$int; } Is there a func...

Retrieving format string from Format object

In Java is there a way to retrieve the format string from a Format object (or any derived classes) In code: Format f = new DecimalFormat("$0.00"); System.out.println(???); Is there something I can use to get System.out.println(???); to print "$0.00". I looked at toPattern(); but that function doesn't appear in the abstract Format c...

What should I use instead of printf in Perl?

I need to use some string replacement in Perl to ease translations, i.e. replace many print "Outputting " . $n . " numbers"; by something like printf ("Outputting %d numbers", $n); However, I'd like to replace printf with something easier to parse for humans, like this: printX ("Outputting {num} numbers", { num => $n }); or gene...

What would be the Regular Expression to retrieve number in front of % symbol in string?

I'm using a math parser that uses % as the mod symbol. I'd like to use it for the percent symbol instead to allow users to type "25%*10" or "25% of 10" and receive the answer "2.5" (they could type anything). I'd then use the regex asked for to get the "25%" (could be in any part of the string) and do a simple calculation (25 / 100) an...