last day (14 days later) » 

5:54 PM
5
A: Money formatting directive in Angular

MeTe-30.:: Updated Answer - July 14 ::. Check this simple directive: app.directive('price', [function () { return { require: 'ngModel', link: function (scope, element, attrs, ngModel) { attrs.$set('ngTrim', "false"); var formatter = function(str, isNum)...

 
Close, but this does not add a decimal after two digits, which should reflect in the view as well as the model.
 
@jenryb You mean, you want to force 2 digit after decimal point?
 
Yes. See behaviour of either of the plunkers to see what I mean
 
@jenryb Ok, no problem
@jenryb Answer updated, i think it's what you want
 
This one is so close. But you still cannot put in a "-" negative sign as the first character (unless the number is already there) An example of this is my second plunker, which allowed the switch between negative and positive. If you can get it working with this I can accept this answer.
Also your plunker does not allow zeros which is a problem too.
If you fix it I will accept your answer and give you the bounty
 
5:54 PM
@jenryb Ok, i'll working on it.
@jenryb Checkout updated answer.
 
Still is not quite working. If you delete the number, you cannot change it to a negative again. Also you cannot toggle between - and + (see first plunker) it is still close
 
@jenryb you mean whenever you hit - or + the whole number sign change?
 
Yes. I noticed that in yours if you type a number, then delete it so it goes back to 0.00, now you are not able to put in a - or + . Also ideally it will make an entire sign change to toggle between. I added a check for this in the first plunker when I edited the question, so it works
 
@jenryb what if its '0.01' and user hit backspace? and when its '0.00' and user hit '-' ?
 
Ideally if its 0.01 and the user hits backspace it would go to an empty field. But if it was 0.00 and user hit '-' then putting a negative in front is ideal
so that you will have -0.00 and as you type -0.01, -0.12 etc. but if you wanted it positive to press '+' and have it turn into 0.12
 
5:56 PM
Ok
 
I'm sorry it is confusing. I hope I can help with any questions.
 
That's Ok. I'll edit the answer in a couple of minutes.
 
6:31 PM
It's updated, checkout pleas
 
7:16 PM
Upon first look, it's looking really good
Yes. I have a couple more questions. How can I take out the comma? or switch the period and comma (in case of french translation where numbers are 7.393,32)
 
replacing 'str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,')' with 'str[0]' will remove comma
there are three places i put '.' in my code and 1 place in above code for ',' . but I'm not sure swapping them will work for you.
 
7:32 PM
hm yes i found that return str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + ',' will change the decimal to a comma, but can't figure out how to replace the comma with a decimal
 
change '$1,' with '$1.'
and replace any '.' in my code with ','
".split('.')" -> ".split(',')"
" + '.' + " -> " + ',' + "
"'0.0'" -> "'0,0'"
Actually i don't know anything about "french translation" to help you more.
I did my best
 
you did! now you get +250 bounty. Thank you :)
 
Thank you too :)
 

last day (14 days later) »