.net 在文本字段中输入货币格式(从右到左)使用 angularJS

.net input currency format in a text field (from right to left)using angularJS

这里是我想要的例子。

U.S中输入的金额。通过在客户输入美元值时显示小数点来显示货币格式。 Example:if 用户输入 1 -> .01 和 1234 -> 12.34。 或 123456 -> 1234.56

提前致谢。

the amount entered in U.S. currency format by displaying a decimal point as customers enter the the dollar value. Example:if user entered 1 -> .01 and 1234 -> 12.34. or 123456 -> 1234.56

根据您的描述,输入的值除以 100 将 return 为所需的值。所以,你可以使用blur事件来改变数据格式,代码如下:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
    <div ng-controller="myCtrl">
        <p>Write something in the input field:</p>
        <input type="text" ng-blur="myFunc()" ng-model="myValue" />
        <p>{{myValue}}</p> 
    </div>

    <script>
  angular.module('myApp', [])
    .controller('myCtrl', ['$scope', function($scope) { 
        $scope.myFunc = function () {
            $scope.myValue = ($scope.myValue / 100).toFixed(2);  
        }; 
    }]);
    </script>
</body>

结果是这样的:

此外,对于货币格式,我们通常用逗号来设置格式,您可以参考这个示例:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
    <div ng-controller="myCtrl">
        <p>Write something in the input field:</p> 
        <input type="text" ng-blur="myFunc2()" ng-model="myValue2" />
        <p>{{myValue2}}</p>
    </div>

    <script>
  angular.module('myApp', [])
    .controller('myCtrl', ['$scope', function($scope) { 
        $scope.myFunc2 = function () { 
            const value = $scope.myValue2.replace(/,/g, '') / 100;
            $scope.myValue2 = parseFloat(value).toLocaleString('en-US', {
                style: 'decimal',
                maximumFractionDigits: 2,
                minimumFractionDigits: 2
            });
        };
    }]);
    </script>
</body>

输出如下:

[注意]通过上面的方法,输入的值除以100就return需要的值,如果输入的值是浮点数,通过使用上面的代码,returned 值将不等于输入的值。如果要防止,去掉除法动作(/100).