Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using System.Globalization.CultureInfo for the purpose.

By using Culture info I access NumberFormatInfo Class.

Which is used in this code snippet.

C#
public static List<CultureInfo> CultureInfoList = new List<CultureInfo>((CultureInfo[])CultureInfo.GetCultures(CultureTypes.SpecificCultures));

        public static NumberFormatInfo CurrentNumberFormat { get; set; }

        public static RegionInfo CurrentRegionInfo { get; set; }

        public static string GetClinicFormattedCurrency(double _amounttobeFormatted)
        {
            try
            {
                var _cultureInfo = CultureInfoList.FirstOrDefault(s => s.Name == "en-US");
                CurrentRegionInfo = new RegionInfo(_cultureInfo.LCID);

                CurrentNumberFormat = _cultureInfo.NumberFormat;

                if (CurrentNumberFormat != null)
                    return _amounttobeFormatted.ToString("c", CurrentNumberFormat);
                else return _amounttobeFormatted.ToString();
            }
            catch (Exception)
            {
                return _amounttobeFormatted.ToString();
            }
        }


Since this fomatting automatically handles rounding, comma-places, negative amounts, etc, Its great.

But the problem is this formatting provides me 1000 as $1,000 but I want USD instead of $.

USD is available in CurrentRegionInfo.ISOCurrencySymbol.


I also want to format the amount based on culture in reoprt/rdlc as well.

for that purpose i'm using this expression:

The solution worked for C# codes and UI display things, but when I want to generate the reports the currency get formatted as per standard logic since it uses system culture.

the expression i use for formatting in report is as follows:

**variable ClinicLanguage value supplied as "en-US"

=CDbl(Parameters!Payment.Value).ToString("C")
=IIF(String.IsNullOrEmpty(Parameters!ClinicLanguage.Value),Parameters!Payment.Value,CDbl(Parameters!Payment.Value).ToString("C"))



Kindly suggest.

Thanks & Regards,
Abhishek
Posted
Updated 17-Jun-14 1:18am
v4

1 solution

Hi Abhishek,

If you don't need to change it programmatically then:

What you can do is changes the regional settings in windows itself, Then in your program it will also change.

Control panel/Region/Formats tab - additional settings/currency tab - currency symbol dropdown (change from $ to USD).

If you need to change it programmatically then:


I copied this from a Microsoft site but you can create your own class and then change the currency symbol

the site is :
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.numberformat(v=vs.110).aspx[^]



I hope this helps!


using System;
using System.Globalization;


public class SamplesCultureInfo {

public static void Main() {

// Creates and initializes a CultureInfo.
CultureInfo myCI = new CultureInfo("en-US", false);

// Clones myCI and modifies the DTFI and NFI instances associated with the clone.
CultureInfo myCIclone = (CultureInfo) myCI.Clone();
myCIclone.DateTimeFormat.AMDesignator = "a.m.";
myCIclone.DateTimeFormat.DateSeparator = "-";
myCIclone.NumberFormat.CurrencySymbol = "USD";
myCIclone.NumberFormat.NumberDecimalDigits = 4;

// Displays the properties of the DTFI and NFI instances associated with the original and with the clone.
Console.WriteLine( "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" );
Console.WriteLine( "DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator );
Console.WriteLine( "DTFI.DateSeparator\t{0}\t\t{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator );
Console.WriteLine( "NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol );
Console.WriteLine( "NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits );

}

}

/*
This code produces the following output.

DTFI/NFI PROPERTY ORIGINAL MODIFIED CLONE
DTFI.AMDesignator AM a.m.
DTFI.DateSeparator / -
NFI.CurrencySymbol $ USD
NFI.NumberDecimalDigits 2 4
 
Share this answer
 
Comments
Abhishek Kumar (here to help!!) 17-Jun-14 7:15am    
The solution worked for C# codes and UI display things, but when I want to generate the reports the currency get formatted as per standard logic since it uses system culture.

the expression i use for formatting in report is as follows:

**variable ClinicLanguage value supplied as "en-US"

=CDbl(Parameters!Payment.Value).ToString("C")
=IIF(String.IsNullOrEmpty(Parameters!ClinicLanguage.Value),Parameters!Payment.Value,CDbl(Parameters!Payment.Value).ToString("C"))

Kindly suggetst how to handle the formatting in reopts as well
Dr Drastic 18-Jun-14 2:35am    
try this

System.Globalization.CultureInfo Cliniclanguage;
Cliniclanguage.NumberFormat.CurrencySymbol = "USD";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900