3

so I am trying to achieve this I have decimal 1.00 Which when i convert to float becomes 1.0. So my question is how can i save the last zero and at the same time to be float not a string if i use number_format(1.0,2) it becomes "1.00" Which is string but i need to be decimal with the last zero. This is what i tried so far

<?php

$decimal = "1.0";
$decimalToFloat = floatval($decimal) // It becomes 1.0
$decimalToFloat = number_format($decimal,2) // It becomes "1.00" !!! String not a float !!!
// The result which i want is 1.00 not "1.00"
5
  • Why does the extra 0 matter unless you are dealing with it as a string (such as printing it out in a report or something)? 1 === 1.0 === 1.00 === 1.0000000 are virtually computationally identical. BTW:.. $decimal = "1.0" is going to make PHP treat $decimal as a STRING. Commented Dec 11, 2017 at 20:39
  • I know, it's the same but i have to send it in this format to Rest API. For instance in C# is possible Commented Dec 11, 2017 at 20:41
  • you do DO need to deal with it as a string of 1.00 is the problem that you are ended up with quotes around it via some middleware or other code? By middlware I'm asking how are you expressing this to the REST API? Are you using some package, writing to STDout.. a stream.. how are you interacting with the API and why does it care if the float looks like: 1.0 ? Sorry, you're question doen't really have enough context to make sense to me. Commented Dec 11, 2017 at 20:42
  • @DDeMartini Man... i did't understand you at all, where you saw middleware ? Look at my previous comment Commented Dec 11, 2017 at 20:43
  • OK forget about the term middleware. There is not enough code there to explain how your are OUTPUTTING the value (echo, print, file_write, carrier pigeon?), and also if the issue youre having is the method you're using to express the data is treating 1.0 as a string because you're treating it as a string (for example, if you are posting the data as JSON, JSON cares about the data type) Commented Dec 11, 2017 at 20:58

1 Answer 1

2

Let me start by saying that this is an XY problem, as you don't need to convert from a string to an integer in PHP, as PHP will coerce the type for you automatically.

In addition to this, there is no need to specify the number of places on a decimal, as 1 is exactly equal to 1.0, and 1.0 is exactly equal to 1.00. In fact, forcibly casting the string showcases that PHP will automatically trim the decimal places:

$decimal = "1.0";
echo number_format((float)$decimal, 2, '.', '');  // "1.00" (string)
echo (int)number_format((float)$decimal, 2, '.', ''); // 1 (int)
echo (float)number_format((float)$decimal, 2, '.', ''); // 1 (float)

It is impossible to showcase an int or float with decimal places comprised purely of zeros in PHP. Though you do not need to do this; you can simply use the whole numbers instead:

echo 1 + 1.01; // 2.01 (float)

Hope this helps! :)

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

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.