Here I am trying to add two decimal values in line var totalSum = (grandTotal + getShippingCost).toFixed(3);
and put the value in var getSumTd = $("tr#sumTr").find("span#sumSpan");
.
But the problem is that var totalSum = (grandTotal + getShippingCost).toFixed(3);
throws an error saying Uncaught TypeError: value.toFixed is not a function
.
Any help with my code will be great help.
Below is my script
<script>
$('button#value-plus').on('click', function () {
debugger;
var divUpd = $(this).closest("tr").find('#qnty');
var subtotalcontainer = $(this).closest("tr").find('span#subtotal');
var mainGrandTotalcontainer = $("tr#mainGtTr").find("#mainGt");
var mainGtVal = parseFloat($("tr#mainGtTr").find('span#shippingCost').text());
var getSumTd = $("tr#sumTr").find("span#sumSpan");
var getShippingCost = parseFloat($("tr#mainGtTr").find('span#mainGt1').text());
var bklId = $(this).closest("tr").find('#pid').val();
var url = "/Product/incrementcart";
$.getJSON(url, { prdid: bklId }, function (data) {
debugger;
divUpd.val(data.qty);
var subTotal = data.qty * data.price;
subtotalcontainer.text(subTotal.toFixed(2));
var grandTotal = (mainGtVal + data.price).toFixed(3);
mainGrandTotalcontainer.text(grandTotal);
var totalSum = (grandTotal + getShippingCost).toFixed(3);
getSumTd.text(totalSum);
}).success(function () {
debugger
var url = "/Product/cartupdate";
$.get(url, function (data) {
debugger;
$(".shopping_button").html(data);
})
});
});
Below is my HTML
<tbody>
@foreach (var item in Model)
{
<tr>
@Html.HiddenFor(model => item.ProductId, htmlAttributes: new { @id = "pid" })
<td data-title="Product Image & name" class="t_md_align_c">
<img src="images/quick_view_img_10.jpg" alt="" class="m_md_bottom_5 d_xs_block d_xs_centered">
<a href="#" class="d_inline_b m_left_5 color_dark">@Html.DisplayFor(modelItem => item.ProductName)</a>
</td>
<td data-title="Stock">
@Html.DisplayFor(modelItem => item.Instock)
</td>
<td data-title="Price">
<p class="f_size_large color_dark">[email protected](modelItem => item.ProductPrice)</p>
</td>
<td data-title="Quantity">
<div class="clearfix quantity r_corners d_inline_middle f_size_medium color_dark m_bottom_10">
<button class="bg_tr d_block f_left" data-direction="down" id="value-minus">-</button>
<input type="text" name="" id="qnty" readonly value="@item.Quantity" class="f_left">
<button class="bg_tr d_block f_left" data-direction="up" id="value-plus">+</button>
</div>
</td>
<td data-title="Subtotal">
<p class="f_size_large fw_medium scheme_color">$<span id="subtotal">@Html.DisplayFor(modelItem => item.Total)</span></p>
</td>
<td data-title="Remove">
<a href="#" class="color_dark"><i class="fa fa-times f_size_medium m_right_5"></i>Remove</a><br>
</td>
</tr>
}
<tr id="mainGtTr">
<td colspan="4" class="v_align_m d_ib_offset_large t_xs_align_l">
<div class="d_ib_offset_0 d_inline_middle half_column d_xs_block w_xs_full m_xs_bottom_5">
<button class="button_type_6 bg_scheme_color f_size_large r_corners tr_all_hover color_light m_bottom_20">Check Out </button>
</div>
<p class="fw_medium f_size_large t_align_r scheme_color p_xs_hr_0 d_inline_middle half_column d_ib_offset_normal d_xs_block w_xs_full t_xs_align_c">Grand Total:</p>
</td>
<td colspan="2" class="v_align_m">
<p class="fw_medium f_size_large scheme_color m_xs_bottom_10">$<span id="mainGt">@ViewBag.SubTotal</span></p>
<p style="font-style:oblique">Include <i class="fa fa-rupee"></i> <span id="shippingCost">@ViewBag.ShipingCost</span> shipping cost</p>
</td>
</tr>
@{
var sum = ViewBag.SubTotal + ViewBag.ShipingCost;
}
<tr id="sumTr">
<td>
<span id="sumSpan">@sum</span>
</td>
</tr>
</tbody>
toFixed()
method formats a number. The current value is of type string and instead of arithmetic addition, string concatenation is happening. Convert those to number before adding:
Change:
var totalSum = (grandTotal + getShippingCost).toFixed(3);
To
var totalSum = (Number(grandTotal) + Number(getShippingCost)).toFixed(3);
Only float, int value have toFixed
. controle your variable and see which type are they.
console.log(("4" + 5).toFixed(3)); // error
console.log((5 + 5).toFixed(3)); // yeep its working
toFixed method is not available on non-number
values. you need to parse value to Number first than you can use toFixed
method.
let str = `123.123456`
console.log(Number(str).toFixed(3))
console.error(str.toFixed(3))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With