Home > Software design >  How can I convert a number into Persian words in Angular or Typescript?
How can I convert a number into Persian words in Angular or Typescript?

Time:01-05

I want to convert numbers to words in my Angular project like this:

1372 --> یک هزار و سیصد و هفتاد و دو

I found a package called persianJS that supports this conversion but it can't be imported in TypeScript, I tried:

import * as persianjs from 'persianjs';

But error says:

Could not find a declaration file for module 'persianjs'. 'd:/Dev/front-end/Dis-dashboard/node_modules/persianjs/persian.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/persianjs` if it exists or add a new declaration (.d.ts) file containing `declare module 'persianjs';`

But there's no @types/persianjs.

Is there any package or function that can be used in Angular?

CodePudding user response:

Ignore the warning by putting // @ts-ignore above your import. It's either that or create the type declaration files yourself.

CodePudding user response:

Instead of persianJS I used persian-tools. first I installed with the command below:

npm install --save @persian-tools/persian-tools

Then I imported this package to my target file:

import * as persianTools from '@persian-tools/persian-tools';

And used it like this:

persianTools.numberToWords("1372") // returns "صد و بیست و سه هزار و دویست"
  •  Tags:  
  • Related