or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

device-preferences.mddevice-settings.mdindex.mdlanguage-matching.mdnumber-currency-formatting.mdplatform-utilities.md

number-currency-formatting.mddocs/

0

# Number and Currency Formatting

1

2

Access to device-specific number formatting preferences and regional currency information for proper localization of numeric data and monetary values.

3

4

## Capabilities

5

6

### Get Number Format Settings

7

8

Retrieves the device's number formatting preferences, including decimal and grouping separators.

9

10

```typescript { .api }

11

/**

12

* Get device's number formatting settings

13

* @returns Object containing decimal and grouping separators

14

*/

15

function getNumberFormatSettings(): NumberFormatSettings;

16

17

interface NumberFormatSettings {

18

/** Character used for decimal separation (e.g., ".", ",") */

19

decimalSeparator: string;

20

/** Character used for thousands grouping (e.g., ",", ".", " ") */

21

groupingSeparator: string;

22

}

23

```

24

25

**Usage Examples:**

26

27

```typescript

28

import { getNumberFormatSettings } from "react-native-localize";

29

30

const formatSettings = getNumberFormatSettings();

31

console.log(formatSettings);

32

// US: { decimalSeparator: ".", groupingSeparator: "," }

33

// DE: { decimalSeparator: ",", groupingSeparator: "." }

34

// FR: { decimalSeparator: ",", groupingSeparator: " " }

35

36

// Format numbers according to device preferences

37

function formatNumber(value: number): string {

38

const { decimalSeparator, groupingSeparator } = getNumberFormatSettings();

39

40

// Use native toLocaleString for proper formatting

41

return value.toLocaleString(undefined, {

42

minimumFractionDigits: 2,

43

maximumFractionDigits: 2

44

});

45

}

46

47

console.log(formatNumber(1234.56));

48

// US: "1,234.56"

49

// DE: "1.234,56"

50

// FR: "1 234,56"

51

```

52

53

**Custom Number Formatting:**

54

55

```typescript

56

import { getNumberFormatSettings } from "react-native-localize";

57

58

function formatCurrency(amount: number, currencyCode: string): string {

59

const { decimalSeparator, groupingSeparator } = getNumberFormatSettings();

60

61

// Manual formatting using device separators

62

const parts = Math.abs(amount).toFixed(2).split('.');

63

const integerPart = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, groupingSeparator);

64

const decimalPart = parts[1];

65

66

const formatted = `${integerPart}${decimalSeparator}${decimalPart}`;

67

const sign = amount < 0 ? '-' : '';

68

69

return `${sign}${currencyCode} ${formatted}`;

70

}

71

72

console.log(formatCurrency(1234.56, "USD"));

73

// US: "USD 1,234.56"

74

// DE: "USD 1.234,56"

75

```

76

77

### Get Device Currencies

78

79

Gets an array of currencies associated with the device's locale preferences.

80

81

```typescript { .api }

82

/**

83

* Get currencies associated with device locales

84

* @returns Array of ISO currency codes (e.g., ["USD", "EUR"])

85

*/

86

function getCurrencies(): string[];

87

```

88

89

**Usage Examples:**

90

91

```typescript

92

import { getCurrencies, getCountry } from "react-native-localize";

93

94

const currencies = getCurrencies();

95

console.log(currencies); // ["USD", "EUR"]

96

97

// Get primary currency

98

const primaryCurrency = currencies[0];

99

console.log(primaryCurrency); // "USD"

100

101

// Use for currency selection

102

function getDefaultCurrency(): string {

103

const currencies = getCurrencies();

104

return currencies[0] || "USD"; // fallback to USD

105

}

106

107

// Currency-aware price formatting

108

function formatPrice(amount: number): string {

109

const currency = getDefaultCurrency();

110

111

return new Intl.NumberFormat(undefined, {

112

style: 'currency',

113

currency: currency

114

}).format(amount);

115

}

116

117

console.log(formatPrice(29.99));

118

// US: "$29.99"

119

// EU: "29,99 €"

120

// UK: "£29.99"

121

```

122

123

**Multi-Currency Support:**

124

125

```typescript

126

import { getCurrencies, getCountry } from "react-native-localize";

127

128

// App with multi-currency support

129

function getSupportedCurrencies(): string[] {

130

const deviceCurrencies = getCurrencies();

131

const appSupportedCurrencies = ["USD", "EUR", "GBP", "JPY", "CAD"];

132

133

// Find intersection of device and app currencies

134

return deviceCurrencies.filter(currency =>

135

appSupportedCurrencies.includes(currency)

136

);

137

}

138

139

// Currency selection with fallback

140

function selectBestCurrency(): string {

141

const supported = getSupportedCurrencies();

142

if (supported.length > 0) {

143

return supported[0];

144

}

145

146

// Fallback based on country

147

const country = getCountry();

148

const countryToCurrency = {

149

"US": "USD",

150

"GB": "GBP",

151

"JP": "JPY",

152

"CA": "CAD"

153

};

154

155

return countryToCurrency[country] || "USD";

156

}

157

```

158

159

**E-commerce Integration:**

160

161

```typescript

162

import { getCurrencies, getNumberFormatSettings } from "react-native-localize";

163

164

class PriceFormatter {

165

private currency: string;

166

private formatSettings: NumberFormatSettings;

167

168

constructor() {

169

this.currency = getCurrencies()[0] || "USD";

170

this.formatSettings = getNumberFormatSettings();

171

}

172

173

formatPrice(amount: number, showCurrency: boolean = true): string {

174

const formatted = new Intl.NumberFormat(undefined, {

175

style: showCurrency ? 'currency' : 'decimal',

176

currency: this.currency,

177

minimumFractionDigits: 2,

178

maximumFractionDigits: 2

179

}).format(amount);

180

181

return formatted;

182

}

183

184

formatDiscount(original: number, discounted: number): string {

185

const savings = original - discounted;

186

const percentage = Math.round((savings / original) * 100);

187

188

return `Save ${this.formatPrice(savings)} (${percentage}%)`;

189

}

190

}

191

192

const formatter = new PriceFormatter();

193

console.log(formatter.formatPrice(99.99)); // "$99.99" or "99,99 €"

194

console.log(formatter.formatDiscount(99.99, 79.99)); // "Save $20.00 (20%)"

195

```