Swift 千分位 金錢價格逗號 千分符

金錢小數點 千位符

金融商品、價格、價位..等有關於金錢的數字,通常通會用標點符號分隔,這種標點符號稱作為 千分位符號(也稱千位分隔符,group separator),譬如:10000->10,000,三位數一個逗點,讓可讀性就變高了。

寫法

let money = 1000000  // 一百萬
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000

取小數點

let money = 1000000.123456  // 一百萬
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2 // 小數點第二位
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000.12

各種樣式

formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000
formatter.numberStyle = .currencyAccounting
print(formatter.string(from: NSNumber(value: money))!)
// ->1,000,000
formatter.numberStyle = .currencyISOCode
print(formatter.string(from: NSNumber(value: money))!)
// -> TWD 1,000,000
formatter.numberStyle = .currencyPlural
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000.00 新台幣
formatter.numberStyle = .decimal
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000
formatter.numberStyle = .none
print(formatter.string(from: NSNumber(value: money))!)
// -> 1000000
formatter.numberStyle = .ordinal
print(formatter.string(from: NSNumber(value: money))!)
// -> 第1,000,000
formatter.numberStyle = .percent
print(formatter.string(from: NSNumber(value: money))!)
// -> 100,000,000%
formatter.numberStyle = .scientific
print(formatter.string(from: NSNumber(value: money))!)
// -> 1E6
formatter.numberStyle = .spellOut
print(formatter.string(from: NSNumber(value: money))!)
// -> 一百萬

各種語言 各種樣式

formatter.locale = Locale(identifier: "ja_JP")
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: money))!)
// -> ¥1,000,000
formatter.locale = Locale(identifier: "zh_TW")
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: money))!)
// -> $1,000,000
formatter.locale = Locale(identifier: "en_MT")
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: money))!)
// -> €1,000,000
formatter.locale = Locale(identifier: "fil_PH")
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: money))!)
// -> ₱1,000,000
formatter.locale = Locale(identifier: "ja_JP")
formatter.numberStyle = .currencyISOCode
print(formatter.string(from: NSNumber(value: money))!)
// -> JPY 1,000,000
formatter.locale = Locale(identifier: "zh_TW")
formatter.numberStyle = .currencyISOCode
print(formatter.string(from: NSNumber(value: money))!)
// -> TWD 1,000,000
formatter.locale = Locale(identifier: "en_MT")
formatter.numberStyle = .currencyISOCode
print(formatter.string(from: NSNumber(value: money))!)
// -> EUR 1,000,000
formatter.locale = Locale(identifier: "fil_PH")
formatter.numberStyle = .currencyISOCode
print(formatter.string(from: NSNumber(value: money))!)
// -> PHP 1,000,000
formatter.locale = Locale(identifier: "ja_JP")
formatter.numberStyle = .currencyPlural
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000 円
formatter.locale = Locale(identifier: "zh_TW")
formatter.numberStyle = .currencyPlural
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000.00 新台幣
formatter.locale = Locale(identifier: "en_MT")
formatter.numberStyle = .currencyPlural
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000.00 euros
formatter.locale = Locale(identifier: "fil_PH")
formatter.numberStyle = .currencyPlural
print(formatter.string(from: NSNumber(value: money))!)
// -> 1,000,000.00 piso ng Pilipinas

其餘地區

可自行替換

mr  Marathi
bs  Bosnian
ee_TG   Ewe (Togo)
ms  Malay
kam_KE  Kamba (Kenya)
mt  Maltese
ha  Hausa
es_HN   Spanish (Honduras)
ml_IN   Malayalam (India)
ro_MD   Romanian (Moldova)
kab_DZ  Kabyle (Algeria)
he  Hebrew
es_CO   Spanish (Colombia)
my  Burmese
es_PA   Spanish (Panama)
az_Latn Azerbaijani (Latin)
mer Meru
en_NZ   English (New Zealand)
xog_UG  Soga (Uganda)
sg  Sango
fr_GP   French (Guadeloupe)
sr_Cyrl_BA  Serbian (Cyrillic, Bosnia and Herzegovina)
hi  Hindi
fil_PH  Filipino (Philippines)
lt_LT   Lithuanian (Lithuania)
si  Sinhala
en_MT   English (Malta)
si_LK   Sinhala (Sri Lanka)
luo_KE  Luo (Kenya)
it_CH   Italian (Switzerland)
teo Teso
mfe Morisyen
sk  Slovak
uz_Cyrl_UZ  Uzbek (Cyrillic, Uzbekistan)
sl  Slovenian
rm_CH   Romansh (Switzerland)
az_Cyrl_AZ  Azerbaijani (Cyrillic, Azerbaijan)
fr_GQ   French (Equatorial Guinea)
kde Makonde
sn  Shona
cgg_UG  Chiga (Uganda)
so  Somali
fr_RW   French (Rwanda)
es_SV   Spanish (El Salvador)
mas_TZ  Masai (Tanzania)
en_MU   English (Mauritius)
sq  Albanian
hr  Croatian
sr  Serbian
en_PH   English (Philippines)
ca  Catalan
hu  Hungarian
mk_MK   Macedonian (Macedonia)
fr_TD   French (Chad)
nb  Norwegian Bokmål
sv  Swedish
kln_KE  Kalenjin (Kenya)
sw  Swahili
nd  North Ndebele
sr_Latn Serbian (Latin)
el_GR   Greek (Greece)
hy  Armenian
ne  Nepali
el_CY   Greek (Cyprus)
es_CR   Spanish (Costa Rica)
fo_FO   Faroese (Faroe Islands)
pa_Arab_PK  Punjabi (Arabic, Pakistan)
seh Sena
ar_YE   Arabic (Yemen)
ja_JP   Japanese (Japan)
ur_PK   Urdu (Pakistan)
pa_Guru Punjabi (Gurmukhi)
gl_ES   Galician (Spain)
zh_Hant_HK  Chinese (Traditional Han, Hong Kong SAR China)
ar_EG   Arabic (Egypt)
nl  Dutch
th_TH   Thai (Thailand)
es_PE   Spanish (Peru)
fr_KM   French (Comoros)
nn  Norwegian Nynorsk
kk_Cyrl_KZ  Kazakh (Cyrillic, Kazakhstan)
kea Kabuverdianu
lv_LV   Latvian (Latvia)
kln Kalenjin
tzm_Latn    Central Morocco Tamazight (Latin)
yo  Yoruba
gsw_CH  Swiss German (Switzerland)
ha_Latn_GH  Hausa (Latin, Ghana)
is_IS   Icelandic (Iceland)
pt_BR   Portuguese (Brazil)
cs  Czech
en_PK   English (Pakistan)
fa_IR   Persian (Iran)
zh_Hans_SG  Chinese (Simplified Han, Singapore)
luo Luo
ta  Tamil
fr_TG   French (Togo)
kde_TZ  Makonde (Tanzania)
mr_IN   Marathi (India)
ar_SA   Arabic (Saudi Arabia)
ka_GE   Georgian (Georgia)
mfe_MU  Morisyen (Mauritius)
id  Indonesian
fr_LU   French (Luxembourg)
de_LU   German (Luxembourg)
ru_MD   Russian (Moldova)
cy  Welsh
zh_Hans_HK  Chinese (Simplified Han, Hong Kong SAR China)
te  Telugu
bg_BG   Bulgarian (Bulgaria)
shi_Latn    Tachelhit (Latin)
ig  Igbo
ses Koyraboro Senni
ii  Sichuan Yi
es_BO   Spanish (Bolivia)
th  Thai
ko_KR   Korean (South Korea)
ti  Tigrinya
it_IT   Italian (Italy)
shi_Latn_MA Tachelhit (Latin, Morocco)
pt_MZ   Portuguese (Mozambique)
ff_SN   Fulah (Senegal)
haw Hawaiian
zh_Hans Chinese (Simplified Han)
so_KE   Somali (Kenya)
bn_IN   Bengali (India)
en_UM   English (U.S. Minor Outlying Islands)
to  Tonga
id_ID   Indonesian (Indonesia)
uz_Cyrl Uzbek (Cyrillic)
en_GU   English (Guam)
es_EC   Spanish (Ecuador)
en_US_POSIX English (United States, Computer)
sr_Latn_BA  Serbian (Latin, Bosnia and Herzegovina)
is  Icelandic
luy Luyia
tr  Turkish
en_NA   English (Namibia)
it  Italian
da  Danish
bo_IN   Tibetan (India)
vun_TZ  Vunjo (Tanzania)
ar_SD   Arabic (Sudan)
uz_Latn_UZ  Uzbek (Latin, Uzbekistan)
az_Latn_AZ  Azerbaijani (Latin, Azerbaijan)
de  German
es_GQ   Spanish (Equatorial Guinea)
ta_IN   Tamil (India)
de_DE   German (Germany)
fr_FR   French (France)
rof_TZ  Rombo (Tanzania)
ar_LY   Arabic (Libya)
en_BW   English (Botswana)
asa Asu
zh  Chinese
ha_Latn Hausa (Latin)
fr_NE   French (Niger)
es_MX   Spanish (Mexico)
bem_ZM  Bemba (Zambia)
zh_Hans_CN  Chinese (Simplified Han, China)
bn_BD   Bengali (Bangladesh)
pt_GW   Portuguese (Guinea-Bissau)
om  Oromo
jmc Machame
de_AT   German (Austria)
kk_Cyrl Kazakh (Cyrillic)
sw_TZ   Swahili (Tanzania)
ar_OM   Arabic (Oman)
et_EE   Estonian (Estonia)
or  Oriya
da_DK   Danish (Denmark)
ro_RO   Romanian (Romania)
zh_Hant Chinese (Traditional Han)
bm_ML   Bambara (Mali)
ja  Japanese
fr_CA   French (Canada)
naq Nama
zu  Zulu
en_IE   English (Ireland)
ar_MA   Arabic (Morocco)
es_GT   Spanish (Guatemala)
uz_Arab_AF  Uzbek (Arabic, Afghanistan)
en_AS   English (American Samoa)
bs_BA   Bosnian (Bosnia and Herzegovina)
am_ET   Amharic (Ethiopia)
ar_TN   Arabic (Tunisia)
haw_US  Hawaiian (United States)
ar_JO   Arabic (Jordan)
fa_AF   Persian (Afghanistan)
uz_Latn Uzbek (Latin)
en_BZ   English (Belize)
nyn_UG  Nyankole (Uganda)
ebu_KE  Embu (Kenya)
te_IN   Telugu (India)
cy_GB   Welsh (United Kingdom)
uk  Ukrainian
nyn Nyankole
en_JM   English (Jamaica)
en_US   English (United States)
fil Filipino
ar_KW   Arabic (Kuwait)
af_ZA   Afrikaans (South Africa)
en_CA   English (Canada)
fr_DJ   French (Djibouti)
ti_ER   Tigrinya (Eritrea)
ig_NG   Igbo (Nigeria)
en_AU   English (Australia)
ur  Urdu
fr_MC   French (Monaco)
pt_PT   Portuguese (Portugal)
pa  Punjabi
es_419  Spanish (Latin America)
fr_CD   French (Congo - Kinshasa)
en_SG   English (Singapore)
bo_CN   Tibetan (China)
kn_IN   Kannada (India)
sr_Cyrl_RS  Serbian (Cyrillic, Serbia)
lg_UG   Ganda (Uganda)
gu_IN   Gujarati (India)
ee  Ewe
nd_ZW   North Ndebele (Zimbabwe)
bem Bemba
uz  Uzbek
sw_KE   Swahili (Kenya)
sq_AL   Albanian (Albania)
hr_HR   Croatian (Croatia)
mas_KE  Masai (Kenya)
el  Greek
ti_ET   Tigrinya (Ethiopia)
es_AR   Spanish (Argentina)
pl  Polish
en  English
eo  Esperanto
shi Tachelhit
kok Konkani
fr_CF   French (Central African Republic)
fr_RE   French (Réunion)
mas Masai
rof Rombo
ru_UA   Russian (Ukraine)
yo_NG   Yoruba (Nigeria)
dav_KE  Taita (Kenya)
gv_GB   Manx (United Kingdom)
pa_Arab Punjabi (Arabic)
es  Spanish
teo_UG  Teso (Uganda)
ps  Pashto
es_PR   Spanish (Puerto Rico)
fr_MF   French (Saint Martin)
et  Estonian
pt  Portuguese
eu  Basque
ka  Georgian
rwk_TZ  Rwa (Tanzania)
nb_NO   Norwegian Bokmål (Norway)
fr_CG   French (Congo - Brazzaville)
cgg Chiga
zh_Hant_TW  Chinese (Traditional Han, Taiwan)
sr_Cyrl_ME  Serbian (Cyrillic, Montenegro)
lag Langi
ses_ML  Koyraboro Senni (Mali)
en_ZW   English (Zimbabwe)
ak_GH   Akan (Ghana)
vi_VN   Vietnamese (Vietnam)
sv_FI   Swedish (Finland)
to_TO   Tonga (Tonga)
fr_MG   French (Madagascar)
fr_GA   French (Gabon)
fr_CH   French (Switzerland)
de_CH   German (Switzerland)
es_US   Spanish (United States)
ki  Kikuyu
my_MM   Burmese (Myanmar [Burma])
vi  Vietnamese
ar_QA   Arabic (Qatar)
ga_IE   Irish (Ireland)
rwk Rwa
bez Bena
ee_GH   Ewe (Ghana)
kk  Kazakh
as_IN   Assamese (India)
ca_ES   Catalan (Spain)
kl  Kalaallisut
fr_SN   French (Senegal)
ne_IN   Nepali (India)
km  Khmer
ms_BN   Malay (Brunei)
ar_LB   Arabic (Lebanon)
ta_LK   Tamil (Sri Lanka)
kn  Kannada
ur_IN   Urdu (India)
fr_CI   French (Côte d’Ivoire)
ko  Korean
ha_Latn_NG  Hausa (Latin, Nigeria)
sg_CF   Sango (Central African Republic)
om_ET   Oromo (Ethiopia)
zh_Hant_MO  Chinese (Traditional Han, Macau SAR China)
uk_UA   Ukrainian (Ukraine)
fa  Persian
mt_MT   Maltese (Malta)
ki_KE   Kikuyu (Kenya)
luy_KE  Luyia (Kenya)
kw  Cornish
pa_Guru_IN  Punjabi (Gurmukhi, India)
en_IN   English (India)
kab Kabyle
ar_IQ   Arabic (Iraq)
ff  Fulah
en_TT   English (Trinidad and Tobago)
bez_TZ  Bena (Tanzania)
es_NI   Spanish (Nicaragua)
uz_Arab Uzbek (Arabic)
ne_NP   Nepali (Nepal)
fi  Finnish
khq Koyra Chiini
gsw Swiss German
zh_Hans_MO  Chinese (Simplified Han, Macau SAR China)
en_MH   English (Marshall Islands)
hu_HU   Hungarian (Hungary)
en_GB   English (United Kingdom)
fr_BE   French (Belgium)
de_BE   German (Belgium)
saq Samburu
be_BY   Belarusian (Belarus)
sl_SI   Slovenian (Slovenia)
sr_Latn_RS  Serbian (Latin, Serbia)
fo  Faroese
fr  French
xog Soga
fr_BF   French (Burkina Faso)
tzm Central Morocco Tamazight
sk_SK   Slovak (Slovakia)
fr_ML   French (Mali)
he_IL   Hebrew (Israel)
ha_Latn_NE  Hausa (Latin, Niger)
ru_RU   Russian (Russia)
fr_CM   French (Cameroon)
teo_KE  Teso (Kenya)
seh_MZ  Sena (Mozambique)
kl_GL   Kalaallisut (Greenland)
fi_FI   Finnish (Finland)
kam Kamba
es_ES   Spanish (Spain)
af  Afrikaans
asa_TZ  Asu (Tanzania)
cs_CZ   Czech (Czech Republic)
tr_TR   Turkish (Turkey)
es_PY   Spanish (Paraguay)
tzm_Latn_MA Central Morocco Tamazight (Latin, Morocco)
lg  Ganda
ebu Embu
en_HK   English (Hong Kong SAR China)
nl_NL   Dutch (Netherlands)
en_BE   English (Belgium)
ms_MY   Malay (Malaysia)
es_UY   Spanish (Uruguay)
ar_BH   Arabic (Bahrain)
kw_GB   Cornish (United Kingdom)
ak  Akan
chr Cherokee
dav Taita
lag_TZ  Langi (Tanzania)
am  Amharic
so_DJ   Somali (Djibouti)
shi_Tfng_MA Tachelhit (Tifinagh, Morocco)
sr_Latn_ME  Serbian (Latin, Montenegro)
sn_ZW   Shona (Zimbabwe)
or_IN   Oriya (India)
ar  Arabic
as  Assamese
fr_BI   French (Burundi)
jmc_TZ  Machame (Tanzania)
chr_US  Cherokee (United States)
eu_ES   Basque (Spain)
saq_KE  Samburu (Kenya)
vun Vunjo
lt  Lithuanian
naq_NA  Nama (Namibia)
ga  Irish
af_NA   Afrikaans (Namibia)
kea_CV  Kabuverdianu (Cape Verde)
es_DO   Spanish (Dominican Republic)
lv  Latvian
kok_IN  Konkani (India)
de_LI   German (Liechtenstein)
fr_BJ   French (Benin)
az  Azerbaijani
guz_KE  Gusii (Kenya)
rw_RW   Kinyarwanda (Rwanda)
mg_MG   Malagasy (Madagascar)
km_KH   Khmer (Cambodia)
gl  Galician
shi_Tfng    Tachelhit (Tifinagh)
ar_AE   Arabic (United Arab Emirates)
fr_MQ   French (Martinique)
rm  Romansh
sv_SE   Swedish (Sweden)
az_Cyrl Azerbaijani (Cyrillic)
ro  Romanian
so_ET   Somali (Ethiopia)
en_ZA   English (South Africa)
ii_CN   Sichuan Yi (China)
fr_BL   French (Saint Barthélemy)
hi_IN   Hindi (India)
gu  Gujarati
mer_KE  Meru (Kenya)
nn_NO   Norwegian Nynorsk (Norway)
gv  Manx
ru  Russian
ar_DZ   Arabic (Algeria)
ar_SY   Arabic (Syria)
en_MP   English (Northern Mariana Islands)
nl_BE   Dutch (Belgium)
rw  Kinyarwanda
be  Belarusian
en_VI   English (U.S. Virgin Islands)
es_CL   Spanish (Chile)
bg  Bulgarian
mg  Malagasy
hy_AM   Armenian (Armenia)
zu_ZA   Zulu (South Africa)
guz Gusii
mk  Macedonian
es_VE   Spanish (Venezuela)
ml  Malayalam
bm  Bambara
khq_ML  Koyra Chiini (Mali)
bn  Bengali
ps_AF   Pashto (Afghanistan)
so_SO   Somali (Somalia)
sr_Cyrl Serbian (Cyrillic)
pl_PL   Polish (Poland)
fr_GN   French (Guinea)
bo  Tibetan
om_KE   Oromo (Kenya)
@farid-alexander

Swift更多文章

Swift 彈出視窗 AlertController 的使用方法 💥

Swift 判斷螢幕方向 📱

Swift Core Data 實現 💾🔥

Swift UISegmentedControl 💻分段控制器!

Swift 實現抽屜效果 🧹


Categorized in: