George M. George M. - 1 year ago 87
JSON Question

Pushing float to object transforms it into a string

I am using the following code:

entries.category[newcat].subcategory[newsub].items[n] = {
name: $("#name").val(),
shortcut: $("#short").val(),
description: $("#desc").val(),
price: parseFloat($("#price").val()).toFixed(2),
vat: parseFloat($("#vat").val()).toFixed(2)

Yet, when I immediately check using
, both
are appended as strings i.e. "10.50" (rather than 10.50).

QUESTION: how do I make sure that price and VAT are stored as float?

Many thanks!

Answer Source

Using toFix(2) coverts the result back to string. To maintain the value as float, you need to pass the result of toFix(2) into another parseFloat

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download