admin管理员组文章数量:1297014
I want to format input to USD currency as you type. The input will have 2 decimal places and will enter from right to left. Suppose if I type 54.60 it will be entered as $0.05-->$0.54-->$5.46-->$54.60. This PLUNKER exactly does this, but its in angular js. So far my directive looks like:
import {Directive, Output, EventEmitter} from '@angular/core';
import {NgControl} from '@angular/forms';
@Directive({
selector: '[formControlName][currency]',
host: {
'(ngModelChange)': 'onInputChange($event)',
'(keydown.backspace)':'onInputChange($event.target.value, true)'
}
})
export class CurrencyMask {
constructor(public model: NgControl) {}
@Output() rawChange:EventEmitter<string> = new EventEmitter<string>();
onInputChange(event: any, backspace: any) {
// remove all mask characters (keep only numeric)
var newVal = event.replace(/\D/g, '');
var rawValue = newVal;
var str = (newVal=='0'?'0.0':newVal).split('.');
str[1] = str[1] || '0';
newVal= str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + '.' + (str[1].length==1?str[1]+'0':str[1]);
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
}
and in html it is being used as:
<input name="cost" placeholder="cost" class="form-control" type="text" currency formControlName="cost" (rawChange)="rawCurrency=$event">
Update:
what finally worked for me is:
onInputChange(event: any, backspace: any) {
var newVal = (parseInt(event.replace(/[^0-9]/g, ''))/100).toLocaleString('en-US', { minimumFractionDigits: 2 });
var rawValue = newVal;
if(backspace) {
newVal = newVal.substring(0, newVal.length - 1);
}
if(newVal.length == 0) {
newVal = '';
}
else {
newVal = newVal;
}
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
I want to format input to USD currency as you type. The input will have 2 decimal places and will enter from right to left. Suppose if I type 54.60 it will be entered as $0.05-->$0.54-->$5.46-->$54.60. This PLUNKER exactly does this, but its in angular js. So far my directive looks like:
import {Directive, Output, EventEmitter} from '@angular/core';
import {NgControl} from '@angular/forms';
@Directive({
selector: '[formControlName][currency]',
host: {
'(ngModelChange)': 'onInputChange($event)',
'(keydown.backspace)':'onInputChange($event.target.value, true)'
}
})
export class CurrencyMask {
constructor(public model: NgControl) {}
@Output() rawChange:EventEmitter<string> = new EventEmitter<string>();
onInputChange(event: any, backspace: any) {
// remove all mask characters (keep only numeric)
var newVal = event.replace(/\D/g, '');
var rawValue = newVal;
var str = (newVal=='0'?'0.0':newVal).split('.');
str[1] = str[1] || '0';
newVal= str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + '.' + (str[1].length==1?str[1]+'0':str[1]);
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
}
and in html it is being used as:
<input name="cost" placeholder="cost" class="form-control" type="text" currency formControlName="cost" (rawChange)="rawCurrency=$event">
Update:
what finally worked for me is:
onInputChange(event: any, backspace: any) {
var newVal = (parseInt(event.replace(/[^0-9]/g, ''))/100).toLocaleString('en-US', { minimumFractionDigits: 2 });
var rawValue = newVal;
if(backspace) {
newVal = newVal.substring(0, newVal.length - 1);
}
if(newVal.length == 0) {
newVal = '';
}
else {
newVal = newVal;
}
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
Share
Improve this question
edited Feb 17, 2017 at 11:34
Jane
asked Feb 7, 2017 at 14:18
JaneJane
2833 gold badges6 silver badges16 bronze badges
2
- could you please state your question? – phil294 Commented Feb 7, 2017 at 14:38
- I want to perform what the stated PLUNKER does in angular 2 , how do I enter the inputs from right to left and format it on the fly – Jane Commented Feb 7, 2017 at 14:41
2 Answers
Reset to default 3on input change use the following
// remove dot and ma's, 123,456.78 -> 12345678
var strVal = myVal.replace(/\.,/g,'');
// change string to integer
var intVal = parseInt(strVal);
// divide by 100 to get 0.05 when pressing 5
var decVal = intVal / 100;
// format value to en-US locale
var newVal = decVal.toLocaleString('en-US', { minimumFractionDigits: 2 });
// or in singel line
var newVal = (parseInt(myVal.replace(/\.,/g, '')) / 100).toLocaleString('en-US', { minimumFractionDigits: 2 });
or
use currency pipe to format to USD format by using only
var newVal = (parseInt(myVal.replace(/\.,/g, '')) / 100)
Hope this helps.
Angular has a formatCurrency method
https://angular.io/api/mon/formatCurrency
Here's how I've used it in my code:
demand is a formControl
formatMoney(value: string) {
this.demand.setValue(
this.formatMoneyBase(value)
);
}
formatMoneyBase(value: string = ''): string {
return value.length ? formatCurrency(parseFloat(value.replace(/\D/g, '')), 'en', '$').replace('$', '') : '';
}
本文标签: javascriptFormat currency input in angular 2Stack Overflow
版权声明:本文标题:javascript - Format currency input in angular 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741648588a2390337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论