TechieClues TechieClues
Updated date Jul 20, 2023
In this blog, we will learn how to use the "C" format specifier to convert numbers to currency strings and display different decimal places. Explore custom currency formatting options and handle currency conversions flawlessly. Moreover, we'll cover internationalization and localization techniques to cater to users worldwide.
  • 37.5k
  • 3
  • 0

Introduction

In today's world, handling currency values is an essential part of many software applications. Whether it's a financial application, an e-commerce platform, or a budgeting tool, formatting currency correctly is crucial for providing a seamless user experience. In the C# programming language, developers have access to a range of powerful tools and techniques to handle currency formatting effectively. This blog aims to explore various methods and best practices for formatting currency in C#, ensuring your applications display monetary values accurately and professionally.

The format specifier "C" (or Currency) is used to converts a number to a string that represents a currency amount. By default, the '$' currency symbol appears when converts a number to a string.

// By default, if you provide only "C", it displays the currency upto two decimal values
string.Format("{0:C}", 1456.12155)    // $1,456.12

C-> X - indicates the decimal places, For example, if display 2 decimal places then use the "C2" currency format specifier.

// "C0" - No decimal values
string.Format("{0:C0}", 1456.12155)   // $1,456
// "C1" - Only one decimal value
string.Format("{0:C1}", 1456.12155)   // $1,456.1
// "C2" - Two decimal values
string.Format("{0:C2}", 1456.12155)   // $1,456.12
// "C3" - Three decimal values
string.Format("{0:C3}", 1456.12155)   // $1,456.122
// "C4" - Four decimal values
string.Format("{0:C4}", 1456.12155)   // $1,456.1216
// "C5" - Five decimal values
string.Format("{0:C5}", 1456.12155)   // $1,456.12155

Display different Currency Symbols:

CultureInfo instance needs to be passed to display the custom culture symbols.

// Default US Currency Format
string.Format(new CultureInfo("en-US"), "{0:C}", 1456.12155)      // $1,456.12
// Currency format for Denmark
string.Format(new CultureInfo("da-DK"), "{0:C}", 1456.12155)      // 1.456,12 kr.
// Currency format for Great Britain
string.Format(new CultureInfo("en-GB"), "{0:C}", 1456.12155)      // £1,456.12
// Currency format for Denmark fo Japan
string.Format(new CultureInfo("ja-JP"), "{0:C}", 1456.12155)      // ¥1,456

Table of countries and their Culture Info Codes:

Country Name Country Code Language Language Code CultureInfo Code
Afghanistan AF Pashto ps ps-AF
Afghanistan AF Dari prs prs-AF
Albania AL Albanian sq sq-AL
Algeria DZ Arabic ar ar-DZ
Argentina AR Spanish es es-AR
Armenia AM Armenian hy hy-AM
Australia AU English en en-AU
Austria AT German de de-AT
Bahrain BH Arabic ar ar-BH
Bangladesh BD Bengali bn bn-BD
Basque ES Basque eu eu-ES
Belarus BY Belarusian be be-BY
Belgium BE French fr fr-BE
Belgium BE Dutch nl nl-BE
Belize BZ English en en-BZ
Bolivarian Republic of Venezuela VE Spanish es es-VE
Bolivia BO Quechua quz quz-BO
Bolivia BO Spanish es es-BO
Brazil BR Portuguese pt pt-BR
Brunei Darussalam BN Malay ms ms-BN
Bulgaria BG Bulgarian bg bg-BG
Cambodia KH Khmer km km-KH
Canada CA French fr fr-CA
Canada CA English en en-CA
Caribbean 29 English en en-029
Catalan ES Catalan ca ca-ES
Chile CL Mapudungun arn arn-CL
Chile CL Spanish es es-CL
Colombia CO Spanish es es-CO
Costa Rica CR Spanish es es-CR
Croatia HR Croatian hr hr-HR
Cyrillic, Azerbaijan AZ Azeri az az-Cyrl-AZ
Cyrillic, Bosnia and Herzegovina BA Serbian sr sr-Cyrl-BA
Cyrillic, Bosnia and Herzegovina BA Bosnian bs bs-Cyrl-BA
Cyrillic, Mongolia MN Mongolian mn mn-MN
Cyrillic, Montenegro ME Serbian sr sr-Cyrl-ME
Cyrillic, Serbia RS Serbian sr sr-Cyrl-RS
Cyrillic, Serbia and Montenegro CS Serbian ) sr sr-Cyrl-CS
Cyrillic, Tajikistan TJ Tajik tg tg-Cyrl-TJ
Cyrillic, Uzbekistan UZ Uzbek uz uz-Cyrl-UZ
Czech Republic CZ Czech cs cs-CZ
Denmark DK Danish da da-DK
Dominican Republic DO Spanish es es-DO
Ecuador EC Quechua quz quz-EC
Ecuador EC Spanish es es-EC
Egypt EG Arabic ar ar-EG
El Salvador SV Spanish es es-SV
Estonia EE Estonian et et-EE
Ethiopia ET Amharic am am-ET
Faroe Islands FO Faroese fo fo-FO
Finland FI Finnish fi fi-FI
Finland FI Swedish sv sv-FI
Finland FI Sami, Northern se se-FI
Finland FI Sami, Skolt sms sms-FI
Finland FI Sami, Inari smn smn-FI
Former Yugoslav Republic of Macedonia MK Macedonian mk mk-MK
France FR French fr fr-FR
France FR Breton br br-FR
France FR Occitan oc oc-FR
France FR Corsican co co-FR
France FR Alsatian gsw gsw-FR
Galician ES Galician gl gl-ES
Georgia GE Georgian ka ka-GE
Germany DE German de de-DE
Germany DE Upper Sorbian hsb hsb-DE
Germany DE Lower Sorbian dsb dsb-DE
Greece GR Greek el el-GR
Greenland GL Greenlandic kl kl-GL
Guatemala GT K'iche qut qut-GT
Guatemala GT Spanish es es-GT
Honduras HN Spanish es es-HN
Hungary HU Hungarian hu hu-HU
Iceland IS Icelandic is is-IS
India IN Hindi hi hi-IN
India IN Bengali bn bn-IN
India IN Punjabi pa pa-IN
India IN Gujarati gu gu-IN
India IN Oriya or or-IN
India IN Tamil ta ta-IN
India IN Telugu te te-IN
India IN Kannada kn kn-IN
India IN Malayalam ml ml-IN
India IN Assamese as as-IN
India IN Marathi mr mr-IN
India IN Sanskrit sa sa-IN
India IN Konkani kok kok-IN
India IN English en en-IN
Indonesia ID Indonesian id id-ID
Iraq IQ Arabic ar ar-IQ
Ireland IE Irish ga ga-IE
Ireland IE English en en-IE
Islamic Republic of Pakistan PK Urdu ur ur-PK
Israel IL Hebrew he he-IL
Italy IT Italian it it-IT
Jamaica JM English en en-JM
Japan JP Japanese ja ja-JP
Jordan JO Arabic ar ar-JO
Kazakhstan KZ Kazakh kk kk-KZ
Kenya KE Kiswahili sw sw-KE
Korea KR Korean ko ko-KR
Kuwait KW Arabic ar ar-KW
Kyrgyzstan KG Kyrgyz ky ky-KG
Lao P.D.R. LA Lao lo lo-LA
Latin, Algeria DZ Tamazight tzm tzm-Latn-DZ
Latin, Azerbaijan AZ Azeri az az-Latn-AZ
Latin, Bosnia and Herzegovina BA Croatian hr hr-BA
Latin, Bosnia and Herzegovina BA Bosnian bs bs-Latn-BA
Latin, Bosnia and Herzegovina BA Serbian sr sr-Latn-BA
Latin, Canada CA Inuktitut iu iu-Latn-CA
Latin, Montenegro ME Serbian sr sr-Latn-ME
Latin, Nigeria NG Hausa ha ha-Latn-NG
Latin, Serbia RS Serbian sr sr-Latn-RS
Latin, Serbia and Montenegro (Former CS Serbian ) sr sr-Latn-CS
Latin, Uzbekistan UZ Uzbek uz uz-Latn-UZ
Latvia LV Latvian lv lv-LV
Lebanon LB Arabic ar ar-LB
Libya LY Arabic ar ar-LY
Liechtenstein LI German de de-LI
Lithuania LT Lithuanian lt lt-LT
Luxembourg LU Luxembourgish lb lb-LU
Luxembourg LU German de de-LU
Luxembourg LU French fr fr-LU
Malaysia MY Malay ms ms-MY
Malaysia MY English en en-MY
Maldives MV Divehi dv dv-MV
Malta MT Maltese mt mt-MT
Mexico MX Spanish es es-MX
Mohawk CA Mohawk moh moh-CA
Monaco MC French fr fr-MC
Morocco MA Arabic ar ar-MA
Nepal NP Nepali ne ne-NP
Netherlands NL Dutch nl nl-NL
Netherlands NL Frisian fy fy-NL
New Zealand NZ Maori mi mi-NZ
New Zealand NZ English en en-NZ
Nicaragua NI Spanish es es-NI
Nigeria NG Yoruba yo yo-NG
Nigeria NG Igbo ig ig-NG
Norway NO Norwegian, Bokmål nb nb-NO
Norway NO Sami, Northern se se-NO
Norway NO Norwegian, Nynorsk nn nn-NO
Norway NO Sami, Lule smj smj-NO
Norway NO Sami, Southern sma sma-NO
Oman OM Arabic ar ar-OM
PRC CN Tibetan bo bo-CN
PRC CN Yi ii ii-CN
PRC CN Uyghur ug ug-CN
Panama PA Spanish es es-PA
Paraguay PY Spanish es es-PY
Peru PE Quechua quz quz-PE
Peru PE Spanish es es-PE
Philippines PH Filipino fil fil-PH
Poland PL Polish pl pl-PL
Portugal PT Portuguese pt pt-PT
Puerto Rico PR Spanish es es-PR
Qatar QA Arabic ar ar-QA
Republic of the Philippines PH English en en-PH
Romania RO Romanian ro ro-RO
Russia RU Russian ru ru-RU
Russia RU Tatar tt tt-RU
Russia RU Bashkir ba ba-RU
Russia RU Yakut sah sah-RU
Rwanda RW Kinyarwanda rw rw-RW
Saudi Arabia SA Arabic ar ar-SA
Senegal SN Wolof wo wo-SN
Simplified, PRC CN Chinese zh zh-CN
Simplified, Singapore SG Chinese zh zh-SG
Singapore SG English en en-SG
Slovakia SK Slovak sk sk-SK
Slovenia SI Slovenian sl sl-SI
South Africa ZA Setswana tn tn-ZA
South Africa ZA isiXhosa xh xh-ZA
South Africa ZA isiZulu zu zu-ZA
South Africa ZA Afrikaans af af-ZA
South Africa ZA Sesotho sa Leboa nso nso-ZA
South Africa ZA English en en-ZA
Spain, International Sort ES Spanish es es-ES
Sri Lanka LK Sinhala si si-LK
Sweden SE Swedish sv sv-SE
Sweden SE Sami, Northern se se-SE
Sweden SE Sami, Lule smj smj-SE
Sweden SE Sami, Southern sma sma-SE
Switzerland CH Romansh rm rm-CH
Switzerland CH German de de-CH
Switzerland CH Italian it it-CH
Switzerland CH French fr fr-CH
Syllabics, Canada CA Inuktitut iu iu-Cans-CA
Syria SY Syriac syr syr-SY
Syria SY Arabic ar ar-SY
Thailand TH Thai th th-TH
Traditional Mongolian, PRC CN Mongolian mn mn-Mong-CN
Traditional, Hong Kong S.A.R. HK Chinese zh zh-HK
Traditional, Macao S.A.R. MO Chinese zh zh-MO
Traditional, Taiwan TW Chinese zh zh-TW
Trinidad and Tobago TT English en en-TT
Tunisia TN Arabic ar ar-TN
Turkey TR Turkish tr tr-TR
Turkmenistan TM Turkmen tk tk-TM
U.A.E. AE Arabic ar ar-AE
Ukraine UA Ukrainian uk uk-UA
United Kingdom GB Welsh cy cy-GB
United Kingdom GB Scottish Gaelic gd gd-GB
United Kingdom GB English en en-GB
United States US English en en-US
United States US Spanish es es-US
Uruguay UY Spanish es es-UY
Vietnam VN Vietnamese vi vi-VN
Yemen YE Arabic ar ar-YE
Zimbabwe ZW English en en-ZW

Conclusion

In conclusion, handling currency formatting is a critical aspect of software development, particularly in financial and e-commerce applications. In C#, developers have access to powerful tools and techniques to ensure accurate and consistent representation of currency values. By understanding the decimal data type, utilizing built-in formatting methods, and implementing custom solutions where necessary, developers can create robust applications that meet the highest standards of currency representation. From basic currency formatting to dealing with rounding and precision challenges, this blog has covered various aspects of currency formatting in C#.

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (3)

    • TechieClues

      TechieClues 13 Mar, 2023

      You have to use like this "string.Format("{0:C}", a);"

    • Jávor Márton

      Jávor Márton 18 Aug, 2023

      This article should help: https://wpf-tutorial.com/data-binding/the-stringformat-property/