Search code examples
javascriptparsefloat

Convert Int to Float Without return String Javascript


I want to convert an int value to a float without returning a string value. If I use parseFloat(value).toFixed(2) it returns a string value. Example "15000.00". I want to return without a string of 15000.00.

Is there any solution?

My expect returns value which is not a string, I need like -> 15000.00 not "15000.00"

The other API requires me to return value to float form. Postman displays {{Key 'estimation_cost' error: 150000 should be instance of 'float'","hint":"Check the request body"}"

Code:

var cost_float = parseFloat(result_2.purchase_request_line[i].estimation_cost).toFixed(2)
result_2.purchase_request_line[i].estimation_cost = cost_float

message": "404 - {"status":"error","msg":" / Key 'estimation_cost' error: 150000 should be instance of 'float'","hint":"Check the request body"}",

My Return "estimation_cost": "150000.00",


Solution

  • There is no such thing as an int in Javascript. All numbers are IEEE 754 floats already.

    If you are trying to round to 2 decimal places, you can do Math.round(x*100)/100.