How to modify currency format using DataFormatString in GridView - asp.net

How would I display the value, for example:
851839.850000
To show its currency equivalent with comma and period formatting:
£851,839.85
by using DataFormatString? Currently I have £{0:c2}, but clearly this is not enough, as it gives me
£851839.850000

It turned out that the original value that was being bound was actually in a string format. Simpling making sure it was incoming as a Decimal allowed the formatting:
£{0:C}
to work precisely as expected and format into a correct decimal value with a £(British Sterling) to be placed before the value.

Define culture like
<%# Page Language="C#" MasterPageFile="~/MasterPage.master"
CodeFile="Search.aspx.cs" Inherits="Search_aspx" Title="Search"
UICulture="hi-IN" Culture="hi-IN" %>
Find culture code from this list

You could try £{0:N2}, see if that works.
Currency formatting is based on the current NumberFormatInfo. C2 ought to work for you, depending on your current culture, but since it doesn't you might want to look at the NumberFormatInfo information.
There's more about currency formatting here: The Currency ("C") Format Specifier

TO display in text box..Try
textbox.text = Format(Value,"$#,###.00")

Related

storing a double type value from html textbox into a c# variable

Does anyone know that how to store a double type value from html textbox into a c# variable?
I have a html textbox with Id txtCost and that will hold a double value.
Thanks in advance.
You need to parse the value using double.Parse() like so:
var value = double.Parse(txtCost.Text);
Although parsing numerical values has some globalization caveats (like how it parses dots and commas). You can read more about the parse method at MSDN
Double val=Convert.ToDouble(txtCost.Text);

How can I make input fields accept locale dependent number formatting?

I'm working on a Spring MVC Project and ran into a problem with the internationalization in forms, especially the number formatting.
I already use fmt:formatNumber to format the numbers according to the current selected locale.
<fmt:formatNumber value="${object[field]}"/>
Like this, number formatting works well when I display numbers. But how about the forms?
At the moment, the input fields that are supposed to receive float values are prefilled with 0.0 and expect me to use "." as decimal separator, no matter what locale is selected. Values containing "," are refused by the server (...can not convert String to required type float...).
How can I make my input fields use and accept the appropriate number format as well?
Did you have a look at #NumberFormat? If you annotate the property the input field is bound to, this should result in the proper formatting. Something like:
#NumberFormat(style = Style.NUMBER)
private BigDecimal something;
This style is the "general-purpose number format for the current locale". I guess, the current locale is determined threadwise from the LocaleContextHolder.
Your app needs to be annotation-driven, also see the section "Annotation-driven Formatting" in the docs.
You might want to take a look at the DecimalFormatSymbols as suggested in this answer.

asp.net label currency format

Hi how do format my label to a currency format.
code
lblFundsAvail.Text = result.Data.FundsAvail.ToString();
thanks
Try using the format specifier C:
lblFundsAvail.Text = result.Data.FundsAvail.ToString("C");
Note though that this will use whatever currency the server happens to have as its current culture, and this may not be the currency you want. You can specify another culture as the second parameter.

MaskedEditExtender, dates and Globalization

I want to use the MaskedEditExtender to mask short dates. The problem is that I want to mask the field depending on the user language settings.
This is working for a lot of cases, but for example for Latvian Culture (with format 9999.99.99. ) is not working.
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AutoComplete="True" MaskType="Date" TargetControlID="myTextbox" ClearMaskOnLostFocus="True"
OnInvalidCssClass="myInvalidCss" OnFocusCssClass="myOnFocusClass" Mask="99/99/9999" >
</cc1:MaskedEditExtender>
Is there a simple way to set the Mask property with the user culture mask format?
Am I missing something to do this easier?
Not sure why the extender won't recognize Latvian culture, but try looking at the overrides provided, such as CultureDateFormat and CultureDecimalPlaceholder. More info at the AJAX Control Toolkit sample website.
EDIT: Response to OP's comments:
I have not idea if this works, but it looks like you can get the short date format for a culture from the CultureInfo class, like this.
string shortDateFormat =
System.Globalization.CultureInfo.DateTimeFormat.ShortDatePattern
Take a look here for examples.

ASP.NET - Negative numbers in Parenthesis

My application is currently displaying negative numbers as -1. The users have changed the requirements (just for a change!) and now we will have to display the numbers as (1).
Can I enable that for the whole application say changing the web.config or even the app's CultureInfo ? Is there any side effect of doing that since we have lots of pages that contain number validators ?
Thanks !
For currency it is really easy:
String.Format("{0:C}", value)
This will use the culture info for the system.
For normal numbers being data bound, use Mark Glorie's sample.
MSDN Article
I'd use String formatting. Making a change to the application's configuration to satisfy a UI requirement is heavy-handed. SteveX wrote a great blog post about String formatting. It's also compatible with markup (aspx) instead of only relevant in code.
From his post:
String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);
This will output “$1,240.00″ if passed 1243.50. It will output the
same format but in parentheses if the number is negative, and will
output the string “Zero” if the number is zero.
Which isn't exactly what you want, but it's close.
Check this..
http://msdn.microsoft.com/en-us/library/91fwbcsb.aspx
Converts the string representation of a number in a specified style to its Decimal equivalent.
I've got the following page bookmarked for doing string formatting: http://idunno.org/archive/2004/14/01/122.aspx
About halfway down, it gives the answer:
String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);
To answer your other question, I wouldn't modify the app.config to make it global, for reasons given in the other answers.
String.Format(”{0:f;(f);0”, -1);
This works.
DataFormatString="{0:c0}"
Nagative amounts in paranthesis
Thousand separater - comma
$ symbol in front
You could always write your own custom ToString() method as an extension method, but like you mention, using CultureInfo is probably better. Take a look here:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbernegativepattern.aspx
Are you displaying your data in Gridview/Datagrids? If so then formatting can be applied per bound-column, something like:
<asp:BoundField DataFormatString="{##;(##)}"/>
This only works with integers however...

Resources