how to show minus currency in brackets - javascript

I have variable which is negative numbers after $ sign (actually it shows currency with currency sign). Please tell me how to show minus currency in brackets with currency sign. I mean to say how to change var val=($125,220,328.00)
My code is looks like this
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function netAmount(){
var net_amount =0;
$('#productList tr:gt(1)').each(function() {
var row_index= $(this).index();
var qty= $('#productList tr:eq('+row_index+') td input[name="quantity"]').val().replace( /[^0-9\.]/g, '' );
var price= $('#productList tr:eq('+row_index+') td input[name="purchase_price"]').val().replace( /[^0-9\.]/g, '' );
net_amount+= +(parseFloat(qty*price).toFixed(2));
$('input[name="net_ammount"]').val('$'+ addCommas(parseFloat(net_amount).toFixed(2)));
});
}
Now i want if net_amount is looks like -123225.32 then it show in input[name="net_ammount"] as ($123,225.32)

Your regexp doesn't match the minus sign, therefore it is not added in the replacement. Change the regexp to this:
var rgx = /(-?\d+)(\d{3})/;

Related

Thousand comma separator issue only last 3 digit is validating

I try to convert the number in text box as comma separated values while typing but only the last values are taken.
<script type="text/javascript">
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(^\d{2})|(\d{1,3})(?=\d{1,3}|$)/g;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
</script>
This is the HTML file:
<asp:TextBox ID="txtbudamt" runat="server" CssClass="text_box" Height="22px" Width="140px" onkeyup="this.value=addCommas(this.value);" onkeydown="return (event.keyCode!=13);" AutoComplete="Off" TabIndex="7"></asp:TextBox>
When I enter numbers till 9999 it gives correct output like 9,999
When I go for 10000 it gives 1,0,000
You can simply use toLocaleString for this. It is also supported on most browsers.
function addCommas(nStr) {
return parseFloat(nStr).toLocaleString('en-GB');
}

How to add a comma to this javascript output?

Here is my counter:
<script type="text/javascript">
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
</script>
And I thought that adding this might insert a comma after the first digit:
(1234567890).toLocaleString();
This could work but not sure how to merge it into the above:
function addCommas(nStr)
{
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Any ideas how to edit this?
Here you have a working example
function formatNumber(num) {
const regex = /\B(?=(\d{3})+(?!\d))/g;
return num.toString().replace(regex, ',');
}
console.log(formatNumber(1));
console.log(formatNumber(100));
console.log(formatNumber(1000));
console.log(formatNumber(1000000));
Why not use Intl.NumberFormat?
console.log(new Intl.NumberFormat('en-EN').format(1234567890));

ng-keyup is not working at rendering side

Here is my code in HTML (Angular):
<input ng-value="minLoanRange" tabindex="1" lable-up id="minLoanRange" class="inputMaterial" ng-model="minLoanRange" ng-disabled="activeType" type="number" max-length-handler required value="" ng-keyup="addCommas(minLoanRange)"/>
Here is my code of addCommas:
$scope.addCommas = function(nStr){
nStr+='';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while(rgx.test(x1)){
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.minLoanRange = x1+x2;
}
I have put an alert just before $scope.minLoanRange = x1+x2; like alert(x1+x2); It is working properly, just not reflecting on screen.
You don't need value="" and ng-value. Because ng-model supports two way binding. if you assign correct value to that model object. its automatically reflect to html.
I have copied the snippet from #lex answer and made some changes
angular.module('app', [])
.controller('controller', function($scope) {
$scope.modelValue = 0;
$scope.commaValue = '';
$scope.addComma = function() {
let nStr = '' + $scope.modelValue.replace(',', '');
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.modelValue = x1 + x2;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input ng-model="modelValue" ng-keyup="addComma()">
</div>
You need to use two different variables - one for the model value and one for the value. Here's a working snippet to demonstrate how you can accomplish what you're after.
angular.module('app', [])
.controller('controller', function($scope) {
$scope.modelValue = 0;
$scope.commaValue = '';
$scope.addComma = function() {
let nStr = '' + $scope.modelValue.replace(',', '');
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.commaValue = x1 + x2;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input ng-value="commaValue" ng-model="modelValue" ng-keyup="addComma()">
</div>

cant figure out how to get the return showing .toFixed(2) amount without error

The script works great for adding items on invoice however i cant figure how to convert the data using .toFixed(2) to show $10.00 instead of 10. I get an error every time I try to add .toFixed(2) . thank you
<script type="text/javascript">
function myFunction() {
var answer = document.getElementById('total');
var x = document.getElementById('itemprice1');
var y = document.getElementById('itemprice2');
var z = document.getElementById('itemprice3');
var w = document.getElementById('itemprice4');
var taxt = document.getElementById('taxtot');
var thetot = document.getElementById('thetot');
// parseFloat converts to values, otherwise you'll concatenate the strings.
answer.value = parseFloat("0" + x.value) + parseFloat("0" + y.value) + parseFloat("0" + z.value) + parseFloat("0" + w.value);
}
function myFunction1() {
var answer = document.getElementById('total');
var taxt = document.getElementById('taxtot');
var thetot = document.getElementById('thetot');
thetot.value = parseFloat("0" + answer.value) + parseFloat("0" + taxt.value);
if (thetot > "0") {
{
//function myFunction2()
var taxt = document.getElementById('taxtot');
var tx1 = document.getElementById('tax1');
var tx2 = document.getElementById('tax2');
var tx3 = document.getElementById('tax3');
var tx4 = document.getElementById('tax4');
var x = document.getElementById('itemprice1');
var y = document.getElementById('itemprice2');
var z = document.getElementById('itemprice3');
var w = document.getElementById('itemprice4');
var answer = document.getElementById('total');
taxt.value = parseFloat("0" + tx1.value) * ("0" + x.value) + parseFloat("0" + tx2.value) * ("0" + y.value) + parseFloat("0" + tx3.value) * ("0" + z.value) + parseFloat("0" + tx4.value) * ("0" + w.value);
}
}
}
</script>
Presumably your controls are in a form, so you can reference them simply using their name in the form. You can also convert strings to numbers using unary +:
function myFunction(formId) {
var f = document.getElementById(formId);
var answer = f.total;
var x = +f.itemprice1.value;
var y = +f.itemprice2.value;
var z = +f.itemprice3.value;
var w = +f.itemprice4.value;
var taxt = f.taxtot;
var thetot = f.thetot;
// Presumably here is where you want to use toFixed
answer.value = '$' + (x + y + z + w).toFixed(2);
}
function to_dollar_string(amount)
{
return "$" + amount.toFixed(2);
}
to_dollar_string(10);
=> "$10.00"
to_dollar_string(10.567);
=> "$10.57"

how to convert * to 000 in currency textbox?

I have currency text box in asp.net
I use :
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>
and
<asp:TextBox ID="amountTextBox" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox>
it works and hasn't any problem, but I want when user press *, instead of * be written '000' in text box. how to do this work?
I hope this is what you need
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
if (Num.indexOf("*") != -1) {
Num = document.getElementById('amountTextBox').value.substring(0, Num.indexOf("*")) + '000';
}
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>

Categories