string-formatting

Using a custom formatter in a DataGridView

So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project. What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value ...

What is the Ruby equivalent of this string formatting?

From Python... s = "hello, %s. Where is %s?" % ("John","Mary") How do you do that in Ruby? ...

PHP remove first zeros

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? ...

Advanced formatting rules for StringBuilder.AppendFormat

I've seen on this site a StringBuilder code sample illustrating AppendFormat usage: using System; using System.Text; class Program { static int[] _v = new int[] { 1, 4, 6 }; static void Main() { StringBuilder b = new StringBuilder(); foreach (int v in _v) { ...

What does "%sUser" and "%d" mean in this code?

PHP function: public static function getById($uid) { $u = new User(); $query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE ' . 'FROM %sUSER WHERE USER_ID = %d', DB_TBL_PREFIX, $uid); $result = mysql_query($query, $GLOBALS['DB']); if (mysql_num_rows($result)) { $row = my...

How do I use string formatting to show BOTH leading zeros and precision of 3?

I'm trying to represent a number with leading and trailing zeros so that the total width is 7 including the decimal point. For example, I want to represent "5" as "005.000". It seems that string formatting will let me do one or the other but not both. Here's the output I get in Ipython illustrating my problem: In [1]: '%.3f'%5 Out[1]: '...

String.Format way to format Currency without Cents

I'm displaying currency using the current method String.Format("{0:C}", item.DonationAmount) Which outputs like $10.00 We will be dealing with large dollar amounts, and no cents. We would like the currency to display as $10 without the decimal or zeroes. How can I do this? Removing the currency format makes the number display like 10...

Formatting applied to boundfields in gridview is not working

I have the following columns in a gridview, one is a date and the other one is a dollar amount. I applied the formatting and set the HtmlEncode property to false, however the values still come up unformatted: <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="true" HtmlEncode="False" DataFormatString="{0:C}" /> <asp:BoundFie...

How to add a parameter to a formatted string function call via a #define function

Hi, I want to create macros that will insert a parameter into a function call. For example, I have function Action() declared below. Action takes as it's inputs an enum for the state number and a formatted string with optional args for the string. I want to define macros so that instead of calling Action( ActionState1, "someText %d"...

double to string formatting

Hi, I have a Double value xx.yyy and I want to convert to string "xxyyy" or "-xxyy", if the value is negative. How could I do it? Regards. ...

"%s" % format vs "{0}".format() vs "?" format.

In this post about SQLite, aaronasterling told me that cmd = "attach \"%s\" as toMerge" % "b.db" : is wrong cmd = 'attach "{0}" as toMerge'.format("b.db") : is correct cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) : is right thing But, I've thought the first and second is the same. What are the differences between tho...

Named parameter string formatting in C++

I'm wondering if there is a library like Boost Format, but which supports named parameters rather than positional ones. This is a common idiom in e.g. Python, where you have a context to format strings with that may or may not use all available arguments, e.g. mouse_state = dict(button=0, x=50, y=30) "You clicked %(button)s at %(x)d,%(y...

Python 2.5 String formatting problem!

I am trying to use the python 2.5 string formatting however I have run into problem in the following example: values = { 'url': 'http://blabla.com', 'link' : 'http://blabla.com', 'username' : 'user', 'spot' : 0, 'views' : 10, 'date' : 3232312, 'private' : 1,...

How to truncate .000 zeros from double with NSNumberFormatter

Ok so if I have a double to begin with how do I limit it or truncate digits to 1dp since I know they're not needed to be viewed double a = 1.6; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:a]]; [numberFormatter setNumberSt...

Is there a way to make XAML / C# StringFormat show whole dollars for larger amounts but cents for smaller amounts?

In XAML, it is easy enough to use StringFormat='$#,0;$-#,0;Nil' to make a bound integer variable display as a nicely formatted dollar amount. e.g., 1024 would come out as '$1,024'. I have a need to deal with numbers ranging from a few cents up to a few hundred dollars - so 0.45 should display as '$0.45', but anything greater than some t...

How can I escape '%' character in a gettext string?

I use gettext to translate my user interface. I'd like to write symbol % as part of a UI caption in a string, but since it has a special meaning, does not works as expected. How can I escape percent symbols? ...

Arbitrary precision number formatting / money_format?

Is there an arbitrary-precision alternative to money_format available that could take a string instead of a float as a parameter? It's not that I plan on doing calculations on trillions of monetary units, but after going through the trouble to properly handle monetary arithmetic without abusing floats, it'd be nice to have a function t...

What would be faster in this context String.Format or String.Replace?

string str = 'my {0} long string {1} need formatting'; Should I be doing the following, str = string.Format(str, "really", "doesn't"); or creating a method like so and calling str = str.ReplaceWithValues("really", "doesn't"); public string ReplaceWithValues(this string str, params object[] values) { string ret = str; for (...

Converting c++ printf formatting to/from VB .NET string formatting

I have some VB .NET software that interfaces to a load of old (but sound) COM objects. The VB provides a GUI for the COM objects, part of which consists of setting various options on the COM objects - several of which relate to string formatting. I have a simple pair of VB .NET functions that convert basic %f, %d, %g formats to/from .NE...

Format Text Going Into A Textbox for a Phone Number, etc.

Hi There, Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to get a textbox to require a certain format as you type in a number. For simplicity, lets use a phone number example. As I type into the box I want some guidelines to help the user enter the correct format of the phone number: (4__) ___-____ ...