Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  116] [ 7]  / answers: 1 / hits: 25408  / 5 Years ago, sun, february 17, 2019, 12:00:00

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 &amp; 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 [email protected] 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>

More From » jquery

 Answers
22

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);

[#52581] Wednesday, February 13, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;