UNPKG

react-native-vietnam-currency

Version:

A simple and flexible utility for formatting Vietnamese Đồng (₫) in React Native — with built-in support for splitting currency and unit, so you can style them differently (e.g., smaller font size for the ₫ symbol).

27 lines (26 loc) 1.03 kB
import React, { useMemo } from "react"; import { Text, View } from "react-native"; export var SeparatorType; (function (SeparatorType) { SeparatorType["Dot"] = "."; SeparatorType["Comma"] = ","; })(SeparatorType || (SeparatorType = {})); const formatNumber = (value, separator) => { const raw = value.toString(); return raw.replace(/\B(?=(\d{3})+(?!\d))/g, separator); }; const Vnd = ({ value, unit = "₫", style, unitStyle, containerStyle, separator = SeparatorType.Dot, }) => { const formatted = useMemo(() => { const numeric = typeof value === "string" ? parseInt(value.replace(/\D/g, ""), 10) : value; if (isNaN(numeric)) return "0"; return formatNumber(numeric, separator); }, [value, separator]); return (<View style={[{ flexDirection: "row", alignItems: "flex-end" }, containerStyle]}> <Text style={[style]}>{formatted}</Text> <Text style={[{ fontSize: 10 }, unitStyle]}>{unit}</Text> </View>); }; export default Vnd;