I'm using a data binding expression in a form and I want to show the bound value as a percentage. My original expression looked something like this:
<%# Eval("commission","{0:p}") %>
However, this doesn't work for me because my value is an integer, not a decimal, and the format string multiplies the value by 100. I did read that putting a single quote in front of a percentage sign would prevent multiplying by 100 (as in string.Format(”{0:##.00′%”,
1.23) will yield 1.23%) but my program gags on this when I use {0:0'%}.
Is my format string incorrect, or does the single quote not work for my situation? Or is there a better way to do the formatting short of dividing my value by 100 before binding it?
The code I sent works (I think) if it's not part of a control. If you were simply adding it as the grid/table cell data. Could you potentially set the text property in the code behind?
Yes, I could set the text property in code behind, but I was trying to avoid doing that. You know, one of those "there's got to be an easy way to do it the way I need to do it" things.
Ah, that works perfectly. I had something like that with the concatenation but I didn't put the integer and conditional format in parentheses. Thanks a bunch.
jasscat
Member
15 Points
34 Posts
Data bound percentage formatting
Apr 06, 2009 02:10 PM|LINK
I'm using a data binding expression in a form and I want to show the bound value as a percentage. My original expression looked something like this:
<%# Eval("commission","{0:p}") %>
However, this doesn't work for me because my value is an integer, not a decimal, and the format string multiplies the value by 100. I did read that putting a single quote in front of a percentage sign would prevent multiplying by 100 (as in string.Format(”{0:##.00′%”, 1.23) will yield 1.23%) but my program gags on this when I use {0:0'%}.
Is my format string incorrect, or does the single quote not work for my situation? Or is there a better way to do the formatting short of dividing my value by 100 before binding it?
MetalAsp.Net
All-Star
112718 Points
18367 Posts
Moderator
Re: Data bound percentage formatting
Apr 06, 2009 02:42 PM|LINK
Does this work?
<%
# String.Format("{0:0'%}", Eval("commision")) %>jasscat
Member
15 Points
34 Posts
Re: Data bound percentage formatting
Apr 06, 2009 03:04 PM|LINK
It doesn't work for me. I'm getting a "server tag not well formed" error when I use:
Text='<%# String.Format("{0:0'%}", Eval("commission")) %>'
Do I have the correct string? Or is String.Format not allowed in this context?
MetalAsp.Net
All-Star
112718 Points
18367 Posts
Moderator
Re: Data bound percentage formatting
Apr 07, 2009 12:53 AM|LINK
The code I sent works (I think) if it's not part of a control. If you were simply adding it as the grid/table cell data. Could you potentially set the text property in the code behind?
limno
All-Star
117426 Points
8030 Posts
Moderator
MVP
Re: Data bound percentage formatting
Apr 07, 2009 02:16 AM|LINK
Text
='<%# Eval("myint","{0:N0}")+"%" %>'Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
jasscat
Member
15 Points
34 Posts
Re: Data bound percentage formatting
Apr 07, 2009 11:56 AM|LINK
Yes, I could set the text property in code behind, but I was trying to avoid doing that. You know, one of those "there's got to be an easy way to do it the way I need to do it" things.
jasscat
Member
15 Points
34 Posts
Re: Data bound percentage formatting
Apr 07, 2009 12:02 PM|LINK
Ah, that works perfectly. I had something like that with the concatenation but I didn't put the integer and conditional format in parentheses. Thanks a bunch.
jstrope
Member
46 Points
42 Posts
Re: Data bound percentage formatting
Apr 26, 2010 05:01 PM|LINK
I got this to work for me by adding a tick mark after the percent sign, like so: {0:0'%'}
I'm also populating a Format property of a Telerik Report TextBox, so that may make a difference.