Javascript INR 货币格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35598765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:57:14  来源:igfitidea点击:

INR currency format

javascriptangularjs

提问by Archana Sharma

I need to show currency format like these, how can we show.

我需要显示这样的货币格式,我们如何显示。

?1
?10
?100
?1,000
?10,000
?1,00,000
......

回答by Sanjay Nishad

In HTML

在 HTML 中

{{ currency_expression | currency : symbol : fractionSize}}

for example

例如

{{amount | currency:"?":0}}

you can also reffer https://docs.angularjs.org/api/ng/filter/currency

你也可以参考 https://docs.angularjs.org/api/ng/filter/currency

回答by codeninja.sj

Indian rupee follows different format compare to US currency; So, don't use default angular currency filter to format Indian Rupees

与美国货币相比,印度卢比遵循不同的格式;所以,不要使用默认的角度货币过滤器来格式化印度卢比

Custom INR Currency Filter

自定义 INR 货币过滤器

var app = angular.module('app', []);

app.controller('indexCtrl', function ($scope) {
    $scope.amount = 10000000.33;
});

app.filter('INR', function () {        
    return function (input) {
        if (! isNaN(input)) {
            var currencySymbol = '?';
            //var output = Number(input).toLocaleString('en-IN');   <-- This method is not working fine in all browsers!           
            var result = input.toString().split('.');

            var lastThree = result[0].substring(result[0].length - 3);
            var otherNumbers = result[0].substring(0, result[0].length - 3);
            if (otherNumbers != '')
                lastThree = ',' + lastThree;
            var output = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
            
            if (result.length > 1) {
                output += "." + result[1];
            }            

            return currencySymbol + output;
        }
    }
});
<!DOCTYPE html>
<html ng-app="app">
<head>    
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="indexCtrl">
    Input: <input type="text" ng-model="amount">
    <h3>{{amount | INR}}</h3>
</body>
</html>

回答by Suhel Khan

Just use INR in currency filter

只需在货币过滤器中使用 INR

{{product.product_cost | currency:'INR':true}}

回答by Himanshu Mishra

You can make a filter using toLocaleStringlike :

您可以使用toLocaleStringlike制作过滤器:

Number(data).toLocaleString('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 0})

Filter example :

过滤器示例:

.filter('IndianCurrency', function() {
    return function(data) {
        if (null != data) {
            return Number(data).toLocaleString('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 0});
        } else {
            return Number(0).toLocaleString('en-IN', { style: 'currency',currency: 'INR', maximumFractionDigits: 0});
        }
    };
});

Usage :

用法 :

{{ rent | IndianCurrency }}

回答by Rohit Arya

To print the rupee symbol(), try like this:

要打印卢比符号(),请尝试如下操作:

{{price | currency:"&#8377;"}}

For more formatting, refer this.

有关更多格式,请参阅

回答by ULLAS K

If any one looking same in angular 2 then here is solution , 1.Create custom pipe 2.Use in your html pipe code

如果任何人在角度 2 中看起来相同,那么这里是解决方案,1.创建自定义管道 2.在您的 html 管道代码中使用

1.Create custom pipe .

1.创建自定义管道。

Create file indianCurrency.pipe.ts

创建文件 indianCurrency.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'indianCurrency'})
export class IndianCurrency implements PipeTransform {
  transform(value: number, args: string[]): any {

        if (! isNaN(value)) {
            var currencySymbol = '?';
            //var output = Number(input).toLocaleString('en-IN');   <-- This method is not working fine in all browsers!           
            var result = value.toString().split('.');

            var lastThree = result[0].substring(result[0].length - 3);
            var otherNumbers = result[0].substring(0, result[0].length - 3);
            if (otherNumbers != '')
                lastThree = ',' + lastThree;
            var output = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;

            if (result.length > 1) {
                output += "." + result[1];
            }            

            return currencySymbol + output;
        }

  }
}

Declare in app.module.ts

在 app.module.ts 中声明

 declarations: [
  .....,
    IndianCurrency
  ],

2.Use in your html

2.在你的html中使用

{{amount | indianCurrency}}

回答by Dilshad


@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  transform(value: number, isSymbol: string, decPointer: string, isPrefix: boolean): string {

    if (!isNaN(value)) {
       var formatted_value;
      var currencyText = isSymbol;
      if (currencyText == '?') {
        // for Indian number system
        formatted_value = new Intl.NumberFormat('en-IN').format(value)
      }
      else {
        formatted_value = new Intl.NumberFormat().format(value)
      }
      return currencyText + ' ' + formatted_value;
    }
  }

}

HTML

HTML

<div>{{ 10000000 | customCurrency:'?':'1.0':false}}</div>

<div>{{ 10000000 | customCurrency:'?':'1.0':false}}</div>

回答by Inderjeet

Indian currency short like if you want to currency view like ? 2.5 Lakh, ? 3 Cr, ?12 Lakh etcthen use below Pipe. It will work

印度货币空头像如果你想货币视图像?2.5 万,?3 Cr、?120 万等然后在管道下方使用。它会工作

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'pricePipe'
})
export class PricePipePipe implements PipeTransform {

  transform(value: number, args: string[]): any {

    if (! isNaN(value)) {
        var currencySymbol = '?';
        if (value == null) {
          return '';
      }
      var InrRSOut = value;
      InrRSOut = Math.round(InrRSOut);
      var RV = "";
      if (InrRSOut > 0 && InrRSOut < 1000) {
          RV = InrRSOut.toString();
      }
      else if (InrRSOut >= 1000 && InrRSOut < 10000) {
          RV = InrRSOut.toString();
      }
      else if (InrRSOut >= 10000 && InrRSOut < 100000) {
          var f1 = InrRSOut.toString().substring(0, 2);
          var f2 = InrRSOut.toString().substring(2, 5);
          RV = f1 + "," + f2;

      }
      else if (InrRSOut >= 100000 && InrRSOut < 1000000) {
          var f1 = InrRSOut.toString().substring(0, 1);
          var f2 = InrRSOut.toString().substring(1, 3);
          if (f2 == "00") {
              RV = f1 + " Lacs";
          }
          else {
              RV = f1 + "." + f2 + " Lacs";
          }
      }
      else if (InrRSOut >= 1000000 && InrRSOut < 10000000) {
          var f1 = InrRSOut.toString().substring(0, 2);
          var f2 = InrRSOut.toString().substring(2, 4);
          if (f2 == "00") {
              RV = f1 + " Lacs";
          }
          else {
              RV = f1 + "." + f2 + " Lacs";
          }
      }
      else if (InrRSOut >= 10000000 && InrRSOut < 100000000) {
          var f1 = InrRSOut.toString().substring(0, 1);
          var f2 = InrRSOut.toString().substring(1, 3);
          if (f2 == "00") {
              RV = f1 + " Cr";
          }
          else {
              RV = f1 + "." + f2 + " Cr";
          }
      }
      else if (InrRSOut >= 100000000 && InrRSOut < 1000000000) {
          var f1 = InrRSOut.toString().substring(0, 2);
          var f2 = InrRSOut.toString().substring(2, 4);
          if (f2 == "00") {
              RV = f1 + " Cr";
          }
          else {
              RV = f1 + "." + f2 + " Cr";
          }
      }
      else if (InrRSOut >= 1000000000 && InrRSOut < 10000000000) {
          var f1 = InrRSOut.toString().substring(0, 3);
          var f2 = InrRSOut.toString().substring(3, 5);
          if (f2 == "00") {
              RV = f1 + " Cr";
          }
          else {
              RV = f1 + "." + f2 + " Cr";
          }
      }
      else if (InrRSOut >= 10000000000) {
          var f1 = InrRSOut.toString().substring(0, 4);
          var f2 = InrRSOut.toString().substring(6, 8);
          if (f2 == "00") {
              RV = f1 + " Cr";
          }
          else {
              RV = f1 + "." + f2 + " Cr";
          }
      }
      else {
          RV = InrRSOut.toString();
      }
      return currencySymbol + RV;
    }

}

}

In HTML Use like

在 HTML 中使用类似

{{ 125000000 | pricePipe }}

Output

输出

?12.50 Cr

回答by Srikrushna

{{myobject.price | currency:'INR'}}

If you don't need last decimal value then you can use number pipe with

如果您不需要最后一个十进制值,那么您可以使用数字管道

?{{myobject.price | number}}

If you don't need last decimal value but you want to use the inbuilt currency pipe functionality, then you can create a custom pipe for currency or you can create a split pipe and use with currency

如果您不需要最后一个十进制值但您想使用内置的货币管道功能,那么您可以为货币创建自定义管道,或者您可以创建一个拆分管道并与货币一起使用

Ex: {{myobject.price  | currency:'INR' | custSplit:".":"0"}}

Note: custSplit- It is a custom pipe it split the string what ever we pass (here- ".") and take the index what we need(Here- "0"Index).

注意:custSplit-它是一个自定义管道,它拆分我们传递的字符串(此处-“。”)并获取我们需要的索引(此处-“0”索引)。

For more on pipe, read angular 2+ doc

有关管道的更多信息,请阅读angular 2+ doc

回答by Paresh Gami

Try this one

试试这个

var x=1000;
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
    lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;

alert(res);