Reputation: 32321
I need to display the values with two decimal format .
When displaying values why the toFixed function is not working in this case
I am getting the following error under browser console
Uncaught TypeError: tax_value.toFixed is not a function
This is my code
var orderjson =
{
"display": {
"ServiceCharge": "1",
"ServiceTax": "2",
"order": [
"ServiceCharge",
"ServiceTax",
"VAT",
"DISCOUNT"
],
"VAT": "3",
"OTH1": "4",
"DISCOUNT": "5"
}
}
var orderOfDisplay = orderjson.display.order;
var ServiceCharge = orderjson.display.ServiceCharge;
var ServiceTax = orderjson.display.ServiceTax;
var VAT = orderjson.display.VAT;
var OTH1 = orderjson.display.OTH1;
var DISCOUNT = orderjson.display.DISCOUNT;
var htmldata = '';
for(var i=0;i<orderOfDisplay.length;i++)
{
var tax_value = orderjson.display[orderOfDisplay[i]];
tax_value = tax_value.toFixed(2);
htmldata+= '<tr><td align="right" valign="middle" width="50%">'+orderOfDisplay[i]+'</td> \
<td align="right" valign="middle" width="50%">Rs. '+tax_value+'</td> \
</tr>';
}
$("#tableid").append(htmldata);
http://jsfiddle.net/2kbet2hL/9/
Could anybody please let me know how to display with two decimal points in this case
Upvotes: 0
Views: 556
Reputation: 797
Please see this I have changed string value to number
var orderjson =
{
"display": {
"ServiceCharge": 1,
"ServiceTax": 2,
"order": [
"ServiceCharge",
"ServiceTax",
"VAT",
"DISCOUNT"
],
"VAT": 3,
"OTH1": 4,
"DISCOUNT": 5
}
}
Js Fiddle:
http://jsfiddle.net/2kbet2hL/10/
Upvotes: 1
Reputation: 48247
Strings don't have a toFixed
method and when you call that, tax_value
is still a string.
For some reason your JSON data has all the numbers encoded as strings ("3"
vs 3
). This isn't really correct, so you should get the services to just return simple numbers.
If you need to force a string into becoming a number, you should use parseInt
or parseFloat
. Your code would change to:
tax_value = parseFloat(tax_value).toFixed(2);
In your current code, using the typeof
operator just before the line with toFixed
will show you the type of tax_value
at that time. In your example, it shows "string"
(and typeof "".toFixed === 'undefined'
, proving the method doesn't exist).
Upvotes: 4