Comments (3)
toLocaleString
does not work with string values
const num = "100000000";
num.toLocaleString(); // 100000000
PersianTools.addCommas(num); // 100,000,000
addCommas
is 15x faster thantoLocaleString
checktoLocaleString
returns a string with the language-sensitive representation of a number
take this for example:
const number = 3500.2;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// → 3.500,2 !!!!
// English version:
// → 3,500.2
addCommas
will always return the same output
from persian-tools.
@ShahriarKh Hi Shahriar
As far as I'm concerned .toLocalString()
is not working on strings and as a result it is not going to work on Persian numbers so I guess that's why addCommos
is implemented. The source code of this function might give you a clue about what's going on:
import { digitsFaToEn } from "../digits";
import { isPersian } from "../isPersian";
/**
* Add Commas to numbers
*
* @method addCommas
* @param {number} number, eg: 300000
* @return {string} string of separated numbers by commas, eg: 30,000
*/
const addCommas = (number: number | string): string => {
if (typeof number !== "number" && typeof number !== "string") return "";
const convertedToString = number.toString();
const tokenizedToEnglish = isPersian(convertedToString)
? (digitsFaToEn(convertedToString) as string)
: convertedToString;
const tokenizedNumber = tokenizedToEnglish.split(".");
const integer = tokenizedNumber[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
const decimal = tokenizedNumber[1] ? `.${tokenizedNumber[1]}` : "";
return integer + decimal;
};
export default addCommas;
Also you can use this link to see other functions.
from persian-tools.
Thanks!
I believe there should be examples of using addCommas
with Persian numbers in the Readme. Honestly, by just checking the examples provided I thought it can only be used with English numbers.
from persian-tools.
Related Issues (20)
- getBankNameFromCardNumber issue HOT 3
- phoneNumberValidator method can't accept phone numbers that start with '0900'' HOT 9
- timeago calculate wrong date HOT 6
- Does @persian-tools support float number? example: 5.3 پنج و سه دهم
- Validate Iranian national number bug HOT 1
- Compiled windows version? HOT 4
- Uncaught RangeError: Maximum call stack size exceeded HOT 6
- phoneNumber must be English digits, not support persian number HOT 1
- divide package to multi packages HOT 4
- not work phoneNumberValidator for 996 prefix HOT 2
- PersianTools: convertToTimeStamp - The input format must be yyyy/mm/dd hh:mm:ss HOT 1
- `phoneNumberDetail` method has incorrect typings HOT 2
- incompatible module with node HOT 1
- phoneNumberValidator does not support all mobile number formats HOT 2
- `verifyCardNumber` util doesn't validate correctly
- `verifyIranianNationalId` util doesn't validate correctly HOT 4
- Wrong result of toPersianChars HOT 4
- getShebaInfo method does not work HOT 5
- maximum call stack size excceded error HOT 1
- NumberToWords HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from persian-tools.