Giter Site home page Giter Site logo

Comments (3)

mediv0 avatar mediv0 commented on June 11, 2024 3
  • toLocaleString does not work with string values
const num = "100000000";
num.toLocaleString();       // 100000000
PersianTools.addCommas(num);     // 100,000,000
  • addCommas is 15x faster than toLocaleString check
  • toLocaleString 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.

pshaddel avatar pshaddel commented on June 11, 2024 2

@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.

ShahriarKh avatar ShahriarKh commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.