|
| 1 | +/*! openspending.amounts.js - Amount formatting for OpenSpending |
| 2 | + * ------------------------------------------------------------------------ |
| 3 | + * |
| 4 | + * Copyright 2013 Open Knowledge Foundation |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +// Define OpenSpending object (if used as a separate module) |
| 20 | +var OpenSpending = OpenSpending || {}; |
| 21 | +// Define Amounts property |
| 22 | +OpenSpending.Amounts = OpenSpending.Amounts || {}; |
| 23 | + |
| 24 | +// Give a short hand version of the amount (with bn, m, k abbreviations) |
| 25 | +OpenSpending.Amounts.shorthand = function (amount) { |
| 26 | + // Define supported amounts |
| 27 | + var billion = 1000000000; |
| 28 | + var million = 1000000; |
| 29 | + var thousand = 1000; |
| 30 | + |
| 31 | + // Get the absolute value (since negative numbers should work as well |
| 32 | + var absolute_amount = Math.abs(amount); |
| 33 | + |
| 34 | + // Billions get 'bn' |
| 35 | + if (absolute_amount > billion) { |
| 36 | + return OpenSpending.Amounts.format(amount / billion) + 'bn'; |
| 37 | + } |
| 38 | + // Millions get 'm' |
| 39 | + else if (absolute_amount > million) { |
| 40 | + return OpenSpending.Amounts.format(amount / million) + 'm'; |
| 41 | + } |
| 42 | + // Thousands get 'k' |
| 43 | + else if (absolute_amount > thousand) { |
| 44 | + return OpenSpending.Amounts.format(amount / thousand) + 'k'; |
| 45 | + } |
| 46 | + // Anything less just returns the amount with two decimal places |
| 47 | + else { |
| 48 | + return OpenSpending.Amounts.format(amount, 2); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +// Format amount using accounting |
| 53 | +OpenSpending.Amounts.format = function (amount, precision, currency) { |
| 54 | + // Get the currency symbol |
| 55 | + currency = OpenSpending.Amounts.currencySymbol(currency); |
| 56 | + // Use accounting.js to format the amount (default precision is 0) |
| 57 | + return accounting.formatMoney(amount, currency, precision||0, |
| 58 | + OpenSpending.localeGroupSeparator, |
| 59 | + OpenSpending.localeDecimalSeparator); |
| 60 | +}; |
| 61 | + |
| 62 | +// Get the currency symbol |
| 63 | +OpenSpending.Amounts.currencySymbol = function (currency) { |
| 64 | + // If currency is defined return currency symbol or currency |
| 65 | + if(currency) { |
| 66 | + return OpenSpending.Amounts.currencySymbols[currency] || currency; |
| 67 | + } else { // If not we just return an empty string |
| 68 | + return ''; |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +// All known currency symbols in openspendingjs |
| 73 | +OpenSpending.Amounts.currencySymbols = { |
| 74 | + "AED": "د.إ", |
| 75 | + "AFN": "؋", |
| 76 | + "ALL": "L", |
| 77 | + "AMD": "դր.", |
| 78 | + "ANG": "ƒ", |
| 79 | + "AOA": "Kz", |
| 80 | + "ARS": "$", |
| 81 | + "AUD": "$", |
| 82 | + "AWG": "ƒ", |
| 83 | + "AZN": "m", |
| 84 | + "BAM": "KM", |
| 85 | + "BBD": "$", |
| 86 | + "BDT": "৳", |
| 87 | + "BGN": "лв", |
| 88 | + "BHD": "ب.د", |
| 89 | + "BIF": "Fr", |
| 90 | + "BMD": "$", |
| 91 | + "BND": "$", |
| 92 | + "BOB": "Bs.", |
| 93 | + "BRL": "R$", |
| 94 | + "BSD": "$", |
| 95 | + "BTN": "Nu", |
| 96 | + "BWP": "P", |
| 97 | + "BYR": "Br", |
| 98 | + "BZD": "$", |
| 99 | + "CAD": "$", |
| 100 | + "CDF": "Fr", |
| 101 | + "CHF": "Fr", |
| 102 | + "CLP": "$", |
| 103 | + "CNY": "¥", |
| 104 | + "COP": "$", |
| 105 | + "CRC": "₡", |
| 106 | + "CUP": "$", |
| 107 | + "CVE": "$, Esc", |
| 108 | + "CZK": "Kč", |
| 109 | + "DJF": "Fr", |
| 110 | + "DKK": "kr", |
| 111 | + "DOP": "$", |
| 112 | + "DZD": "د.ج", |
| 113 | + "EEK": "KR", |
| 114 | + "EGP": "£,ج.م", |
| 115 | + "ERN": "Nfk", |
| 116 | + "ETB": "Br", |
| 117 | + "EUR": "€", |
| 118 | + "FJD": "$", |
| 119 | + "FKP": "£", |
| 120 | + "GBP": "£", |
| 121 | + "GEL": "ლ", |
| 122 | + "GHS": "₵", |
| 123 | + "GIP": "£", |
| 124 | + "GMD": "D", |
| 125 | + "GNF": "Fr", |
| 126 | + "GTQ": "Q", |
| 127 | + "GYD": "$", |
| 128 | + "HKD": "$", |
| 129 | + "HNL": "L", |
| 130 | + "HRK": "kn", |
| 131 | + "HTG": "G", |
| 132 | + "HUF": "Ft", |
| 133 | + "IDR": "Rp", |
| 134 | + "ILS": "₪", |
| 135 | + "INR": "₨", |
| 136 | + "IQD": "ع.د", |
| 137 | + "IRR": "﷼", |
| 138 | + "ISK": "kr", |
| 139 | + "JMD": "$", |
| 140 | + "JOD": "د.ا", |
| 141 | + "JPY": "¥", |
| 142 | + "KES": "KSh", |
| 143 | + "KGS": "лв", |
| 144 | + "KHR": "៛", |
| 145 | + "KMF": "Fr", |
| 146 | + "KPW": "₩", |
| 147 | + "KRW": "₩", |
| 148 | + "KWD": "د.ك", |
| 149 | + "KYD": "$", |
| 150 | + "KZT": "Т", |
| 151 | + "LAK": "₭", |
| 152 | + "LBP": "ل.ل", |
| 153 | + "LKR": "ரூ", |
| 154 | + "LRD": "$", |
| 155 | + "LSL": "L", |
| 156 | + "LTL": "Lt", |
| 157 | + "LVL": "Ls", |
| 158 | + "LYD": "ل.د", |
| 159 | + "MAD": "د.م.", |
| 160 | + "MDL": "MDL", |
| 161 | + "MGA": "Ar", |
| 162 | + "MKD": "ден", |
| 163 | + "MMK": "K", |
| 164 | + "MNT": "₮", |
| 165 | + "MOP": "P", |
| 166 | + "MRO": "UM", |
| 167 | + "MUR": "₨", |
| 168 | + "MVR": "ރ.", |
| 169 | + "MWK": "MK", |
| 170 | + "MXN": "$", |
| 171 | + "MYR": "RM", |
| 172 | + "MZN": "MT", |
| 173 | + "NAD": "$", |
| 174 | + "NGN": "₦", |
| 175 | + "NIO": "C$", |
| 176 | + "NOK": "kr", |
| 177 | + "NPR": "₨", |
| 178 | + "NZD": "$", |
| 179 | + "OMR": "ر.ع.", |
| 180 | + "PAB": "B/.", |
| 181 | + "PEN": "S/.", |
| 182 | + "PGK": "K", |
| 183 | + "PHP": "₱", |
| 184 | + "PKR": "₨", |
| 185 | + "PLN": "zł", |
| 186 | + "PYG": "₲", |
| 187 | + "QAR": "ر.ق", |
| 188 | + "RON": "RON", |
| 189 | + "RSD": "RSD", |
| 190 | + "RUB": "р.", |
| 191 | + "RWF": "Fr", |
| 192 | + "SAR": "ر.س", |
| 193 | + "SBD": "$", |
| 194 | + "SCR": "₨", |
| 195 | + "SDG": "S$", |
| 196 | + "SEK": "kr", |
| 197 | + "SGD": "$", |
| 198 | + "SHP": "£", |
| 199 | + "SLL": "Le", |
| 200 | + "SOS": "Sh", |
| 201 | + "SRD": "$", |
| 202 | + "STD": "Db", |
| 203 | + "SYP": "£, ل.س", |
| 204 | + "SZL": "L", |
| 205 | + "THB": "฿", |
| 206 | + "TJS": "ЅМ", |
| 207 | + "TMT": "m", |
| 208 | + "TND": "د.ت", |
| 209 | + "TOP": "T$", |
| 210 | + "TRY": "₤", |
| 211 | + "TTD": "$", |
| 212 | + "TWD": "$", |
| 213 | + "TZS": "Sh", |
| 214 | + "UAH": "₴", |
| 215 | + "UGX": "Sh", |
| 216 | + "USD": "$", |
| 217 | + "UYU": "$", |
| 218 | + "UZS": "лв", |
| 219 | + "VEF": "Bs", |
| 220 | + "VND": "₫", |
| 221 | + "VUV": "Vt", |
| 222 | + "WST": "T", |
| 223 | + "XAF": "Fr", |
| 224 | + "XCD": "$", |
| 225 | + "XOF": "Fr", |
| 226 | + "XPF": "Fr", |
| 227 | + "YER": "﷼", |
| 228 | + "ZAR": "R", |
| 229 | + "ZMK": "ZK", |
| 230 | + "ZWL": "$" |
| 231 | +}; |
| 232 | + |
0 commit comments