2015-12-26 68 views
0

我想將數字轉換爲數字格式。格式編號爲角錢格式

我已經試過這至今:http://plnkr.co/edit/lVJGXyuX0BMvB9QUL5zS?p=preview

function formatMoney(credits) { 

    console.log(credits+'credits'); 
    var lastThree = credits.substring(credits.length-3); 
    // var lastThree = credits.slice(-2); 
    var otherNumbers = credits.substring(0,credits.length-3); 
    console.log(otherNumbers+'otherNumbers'); 
    if(otherNumbers !== '') 
     lastThree = ',' + lastThree; 
    var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree; 
    return res; 
    } 

function formatNumber(num) { 
    var n1, n2; 
    num = (Math.round(num * 100)/100) + '' || ''; 
    n1 = num.split('.'); 
    n2 = n1[1] || null; 
    n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ","); 
    num = n2 ? n1 + '.' + n2 : n1; 
    return num; 
    } 

我想將數字轉換爲印度貨幣。

實施例:

1,000 
10,000 
1,00,000 
10,00,000 

,我與功能formatMoney()獲得的輸出:1,0,0,000

,我與功能formatNumber()獲得的輸出:1000和壓制後0,它變成NaN0

我在這裏做錯了什麼?

+0

這已經回答。 http://stackoverflow.com/questions/16037165/displaying-a-number-in-indian-format-using-javascript 如果這不起作用,生病會很高興給它一個去。 – Conceptz

+0

@Conceptz我可能是錯的,在這裏是相當新的,但我認爲這個問題是基於angularJS的不同的答案,給出了它如何被標記 –

+0

@Conceptz:我試了相同的代碼,不適合我。我在這裏做錯了什麼? – nirvair

回答

2

AngularJS商做這些事情的過濾器,還有一個currency filter可供選擇:

我已經重寫你的代碼刪除,並與貨幣過濾器加上印度貨幣盧比符號代替了格式化功能,希望這解決您的問題。

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

 
app.controller('MainCtrl', function($scope) { 
 
    
 
    $scope.change=function($event, money){ 
 
     $scope.money = qwerty; 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.20/angular.js" data-semver="1.2.20"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <input ng-keypress="change($event, money)" type="text" ng-model='money' > 
 
    <pre>{{money | currency : "&#8377;" }}</pre> 
 
    </body> 
 

 
</html>

+0

我想在輸入框本身格式化數字,而不是以HTML格式顯示。 – nirvair

+0

{{money |貨幣:「₹」}} <<移動此 Conceptz

+0

I don不需要盧比符號。我只需要格式化數字。就像您在之前的評論中提供的鏈接。 – nirvair

0

您可能注入和直接使用$filter服務到您的控制器,甚至你的觀點

var myApp = angular.module('myApp', []); 
myApp.controller('CalcCtrl', function ($scope, $filter) { 
    $scope.num = 122212111221121212; 
    $scope.filtered = $filter('currency')($scope.num); 
    alert($scope.filtered) 
}); 

請記住,在$過濾服務是很容易得延長。

myApp.filter('customCurrency', function() { 

    return function(input, symbol, place) { 

    // Ensure that we are working with a number 
    if(isNaN(input)) { 
     return input; 
    } else { 

     // Check if optional parameters are passed, if not, use the defaults 
     var symbol = symbol || 'EUROS'; 
     var place = place === undefined ? true : place; 

     // Perform the operation to set the symbol in the right location 
     if(place === true) { 
     return symbol + input; 
     } else { 
     return input + symbol; 
     } 

    } 
    } 

}); 

myApp.controller('CalcCtrl', function ($scope, $filter) { 
    $scope.num = 122212111221121212; 
    $scope.filtered = $filter('customCurrency')($scope.num); 
}); 

Demo

+0

但是我的方法有什麼問題? – nirvair

+0

@phantomphoenix Angular使用服務將常用功能注入到控制器或視圖中。您的函數方法編碼良好,但由於範圍限制,仍然無法在控制器之外使用。 – Theodore

+0

我不想在控制器外部使用它。我想知道這個代碼有什麼問題。 – nirvair