1

I have a string ($maxDeposit) which is a numeric monetary value. So, for example:

123.00

This string is being passed in to jQuery, it needs to be passed in as a numeric data type. I'm achieving this using the following:

$maxDeposit = floatval($maxDeposit);

This loses the last last decimal place however, so my number looks like:

123.0

I have this method of converting the number to two decimal places:

$maxDeposit =  sprintf('%0.2f', round($maxDeposit, 2));

However this also converts the number back to a string. Is there a way I can convert the string to a float but keep the last decimal place? Thanks

1
  • 1
    Do that in javascript parseFloat() If you are sending data to javascript as JSON its going to be converted to a string anyway Commented Jan 6, 2016 at 11:11

2 Answers 2

2

No, float is a numeric value, and 123.00 is its representation with 2 decimal places. It is responsibility of view layer to format numbers. In your case it is jQuery, e.g. console.log(maxDeposit.toFixed(2)).

Sign up to request clarification or add additional context in comments.

Comments

1

I think, You can use floatval/float and number_format.

$maxDeposit = number_format(floatval($maxDeposit), 2);

or

number_format((float)$maxDeposit, 2, '.', '');

http://php.net/manual/pt_BR/function.number-format.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.