Storing Currency Symbols in a Database Table

∥☆過路亽.° 提交于 2019-12-01 05:56:44

Issues with this approach

If you need to output currency text, I would recommend that you do not store the currency symbol associated with the currency data. Even if the symbol is stored in another column and you pull it out as needed. Rather a better approach would be to store the region / locale for the currency data then, format the currency using the regions default formatting. If whatever language/platform you are using doesn't have locale based currency functionality, I still think this is a better approach. In that case, you would do the formatting in a bit of application logic and then display it to the user.

The main reasoning being this is that in different regions and locales not only is the symbol different, but so is the placement of the symbol and the formatting of the currency itself. Also think if you need to distinguish between different types of dollars - maybe at some point you will want to indicate if the dollar amount is in USD, HKD, AUD, etc.

Currency examples

  1.234,56€ - Euro (Germany)
€ 1.234,56  - Euro (Netherlands)
€ 1,234.56  - Euro (Ireland)
  123,456円 - Yen (Japan)
  123,456¥ - Yen (Japan) - yes there are two different currency marks!

Other resources

Andreas Petersson

I would point to two main pitfalls:

First, make sure your database is correctly installed as utf-8. Some database force you to chose this at install time, others at schema/user creation time.

Second, make sure the programming language you use supports utf-8. php is especially known to cause problems whith string manipulation and utf-8.

The create database statement for firebird looks as the following:

Make sure you use at least version 2.0 of firebird, the first version to support utf-8.

      CREATE DATABASE <database> 
      USER <username> 
      PASSWORD <password> 
      PAGE_SIZE <pagesize> 
      DEFAULT CHARACTER SET UTF8

Alternatively you can create a single field with

      CREATE TABLE money (
       SYMBOL VARCHAR(50) CHARACTER SET UTF8 )

Edit: To clarify on what to store: you store exactly the characters as they appear on the screen. Most likely you can just cut/paste them from any program to your database or inside a sql statement. No need to fumble around with conversions to int/hex/oct/ascii whatsoever.

Edit2: Of course you do not store the fully formatted number, but rather only the single currency symbols.

It may also be a Font issue, some Fonts do not have all rare utf-8 characters. For example Arial is missing it. "Lucida Sans Unicode" definately has it. Try setting "Lucida Sans Unicode" in your viewing program.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!