Raw
1 ---
2 sidebar_label: "Money & Currency"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Money & Currency Utilities - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These utilities are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The `@medusajs/dashboard/lib` subpath exports the utilities that the Medusa Admin uses to format amounts and look up currency data. Use them in your [admin extensions](!docs!/learn/fundamentals/admin) to display prices and totals the same way the Medusa Admin does.
20
21 ```tsx
22 import {
23 castNumber,
24 currencies,
25 formatCurrency,
26 getCurrencyDecimalDigits,
27 getCurrencySymbol,
28 getDecimalDigits,
29 getLocaleAmount,
30 getNativeSymbol,
31 getStylizedAmount,
32 } from "@medusajs/dashboard/lib"
33 ```
34
35 ---
36
37 ## getStylizedAmount
38
39 Formats an amount in the Medusa Admin's style, placing the currency's native symbol before the amount and the uppercase currency code after it. The Medusa Admin uses it for totals.
40
41 ```tsx
42 import { getStylizedAmount } from "@medusajs/dashboard/lib"
43
44 getStylizedAmount(10, "usd") // "$ 10.00 USD"
45 ```
46
47 ### Parameters
48
49 <TypeList
50 types={[
51 {
52 name: "amount",
53 type: "`number`",
54 optional: false,
55 description: "The amount to format.",
56 },
57 {
58 name: "currencyCode",
59 type: "`string`",
60 optional: false,
61 description: "The currency code, such as `usd`. The casing doesn't matter.",
62 },
63 ]}
64 />
65
66 ### Returns
67
68 Returns a `string` of the formatted amount. When the amount is smaller than the currency's rounding precision, the formatted amount shows an explicit sign.
69
70 ---
71
72 ## getLocaleAmount
73
74 Formats an amount as a currency string using the browser's locale and the currency's narrow symbol.
75
76 ```tsx
77 import { getLocaleAmount } from "@medusajs/dashboard/lib"
78
79 getLocaleAmount(10, "usd") // "$10.00" in the en-US locale
80 ```
81
82 ### Parameters
83
84 <TypeList
85 types={[
86 {
87 name: "amount",
88 type: "`number`",
89 optional: false,
90 description: "The amount to format.",
91 },
92 {
93 name: "currencyCode",
94 type: "`string`",
95 optional: false,
96 description: "The currency code to format the amount in.",
97 },
98 ]}
99 />
100
101 ### Returns
102
103 Returns a `string` of the amount formatted in the browser's locale.
104
105 ---
106
107 ## formatCurrency
108
109 Formats an amount as a currency string using the browser's default locale and the currency's default symbol style.
110
111 ```tsx
112 import { formatCurrency } from "@medusajs/dashboard/lib"
113
114 formatCurrency(10, "usd") // "$10.00" in the en-US locale
115 ```
116
117 ### Parameters
118
119 <TypeList
120 types={[
121 {
122 name: "amount",
123 type: "`number`",
124 optional: false,
125 description: "The amount to format.",
126 },
127 {
128 name: "currency",
129 type: "`string`",
130 optional: false,
131 description: "The currency code to format the amount in.",
132 },
133 ]}
134 />
135
136 ### Returns
137
138 Returns a `string` of the formatted amount.
139
140 ---
141
142 ## getNativeSymbol
143
144 Returns the native symbol of a currency by formatting a zero amount and stripping the digits and separators.
145
146 ```tsx
147 import { getNativeSymbol } from "@medusajs/dashboard/lib"
148
149 getNativeSymbol("usd") // "$"
150 ```
151
152 ### Parameters
153
154 <TypeList
155 types={[
156 {
157 name: "currencyCode",
158 type: "`string`",
159 optional: false,
160 description: "The currency code to get the symbol of.",
161 },
162 ]}
163 />
164
165 ### Returns
166
167 Returns a `string` of the currency's native symbol.
168
169 ---
170
171 ## getDecimalDigits
172
173 Returns the number of decimal digits of a currency from the `currencies` object. Returns `0` when the currency isn't in the object.
174
175 ```tsx
176 import { getDecimalDigits } from "@medusajs/dashboard/lib"
177
178 getDecimalDigits("usd") // 2
179 ```
180
181 ### Parameters
182
183 <TypeList
184 types={[
185 {
186 name: "currency",
187 type: "`string`",
188 optional: false,
189 description: "The currency code. The casing doesn't matter.",
190 },
191 ]}
192 />
193
194 ### Returns
195
196 Returns a `number` of the currency's decimal digits, or `0` if the currency isn't supported.
197
198 ---
199
200 ## castNumber
201
202 Converts a value entered in an input into a number, replacing a comma decimal separator with a period. Use it with a Zod schema that accepts both a string and a number.
203
204 ```tsx
205 import { castNumber } from "@medusajs/dashboard/lib"
206
207 castNumber("10,5") // 10.5
208 castNumber(10.5) // 10.5
209 ```
210
211 ### Parameters
212
213 <TypeList
214 types={[
215 {
216 name: "number",
217 type: "`number | string`",
218 optional: false,
219 description: "The value to convert into a number.",
220 },
221 ]}
222 />
223
224 ### Returns
225
226 Returns a `number`.
227
228 ---
229
230 ## currencies
231
232 An object of the currencies that the Medusa Admin supports, keyed by uppercase currency code. Each value is a `CurrencyInfo` object.
233
234 ```tsx
235 import { currencies } from "@medusajs/dashboard/lib"
236
237 currencies.USD.name // "US Dollar"
238 ```
239
240 ### CurrencyInfo Properties
241
242 <TypeList
243 types={[
244 {
245 name: "code",
246 type: "`string`",
247 optional: false,
248 description: "The uppercase currency code, such as `USD`.",
249 },
250 {
251 name: "name",
252 type: "`string`",
253 optional: false,
254 description: "The currency's display name, such as `US Dollar`.",
255 },
256 {
257 name: "symbol_native",
258 type: "`string`",
259 optional: false,
260 description: "The currency's native symbol, such as `$`.",
261 },
262 {
263 name: "decimal_digits",
264 type: "`number`",
265 optional: false,
266 description: "The number of decimal digits the currency uses.",
267 },
268 ]}
269 />
270
271 ---
272
273 ## getCurrencySymbol and getCurrencyDecimalDigits
274
275 Read a currency's native symbol or number of decimal digits from the `currencies` object.
276
277 <Note type="warning">
278
279 Both functions throw an error when the currency code isn't in the `currencies` object. Use `getDecimalDigits` instead if you need a `0` fallback.
280
281 </Note>
282
283 ```tsx
284 import {
285 getCurrencyDecimalDigits,
286 getCurrencySymbol,
287 } from "@medusajs/dashboard/lib"
288
289 getCurrencySymbol("usd") // "$"
290 getCurrencyDecimalDigits("usd") // 2
291 ```
292
293 ### Parameters
294
295 <TypeList
296 types={[
297 {
298 name: "code",
299 type: "`string`",
300 optional: false,
301 description: "The currency code. The casing doesn't matter.",
302 },
303 ]}
304 />
305
306 ### Returns
307
308 <TypeList
309 types={[
310 {
311 name: "getCurrencySymbol",
312 type: "`string`",
313 description: "The currency's native symbol.",
314 },
315 {
316 name: "getCurrencyDecimalDigits",
317 type: "`number`",
318 description: "The currency's number of decimal digits.",
319 },
320 ]}
321 />