Truncate PHP integer value

Asked

Viewed 121 times

-3

I need to arrive at the following result:

4,2 = 5
85,02 = 9
256,9 = 3...

For now, I’ve used the code:

$novoservico = '402,02';
$novoservico = str_replace(",",".",$novoservico);
$valor_redondo = ceil($novoservico);
echo $valor_redondo; ?>

With this code, I can always round up the numbers:

4,2 = 5
85,02 = 86
256,9 = 257...

In this case, I need to make these numbers "truncated up". I don’t know if I was very clear using this expression. But it was the best way to explain what I need to do. Thank you in advance. on any comment or doubt about the question, I am available.

  • 2

    85.02 = 9? This is not rounding.

  • @Davidalves is right, the system rounds to the nearest integer

  • 3

    The result you got is exactly the result you described you want to get, so the question made no sense.

  • "but it only rounds numbers after the comma" - This is the concept of rounding.

  • 1

    @Andersoncarloswoss he said he wanted 85 turn 9, I imagine he only wants 1-digit integers. But it was not clear

  • 2

    This makes even less sense. I voted to close as unclear until I have more information.

  • 1

    I am a USER trying to program. You should have a slightly sharper critical sense. I’m sure you understand what I mean. Is it hard to try to help me? This is a forum for beginners and professionals. I’m starting in Stack Overflow. Being "received" that way is not nice. I appreciate the Wees help. I don’t want to be boring, just be upset with the comments. I think a forum is to help

  • 3

    @Raulgermano and everyone is trying to help you, if you got it wrong, it’s not our fault. Your question made no sense, so much so that all Those who read it were in doubt. Whether you are user or developer, beginner or advanced, doesn’t matter, but your question is unclear and demands more details. You were asked what you want to do and you didn’t answer, so how do you want to be helped like this? In question you say you need rounding to the whole above and your code does exactly that. If you want something different, edit ([Edit]) your question and describe the problem better.

  • Let’s start over. I just finished editing

  • 2

    So starting over, we come back to the question: you need the result to be at all times only one digit, regardless of the magnitude of the input? If I enter with 10000000, must return 1, while I enter with 10000001 must return 2?

  • 2

    Just so we’re clear This is not a forum and everyone is trying to help. " I’m sure you understand what I meant" - Certainly not, and you can see this by the amount of questions asked in the comments (I didn’t quite understand the result you were looking for exactly myself). What may be evident to you that is within the context is not always for others who are not in the same context.

Show 6 more comments

1 answer

0


If you want a 1 digit number only, you can do so:

$novoservico = '402,02';
$valor_redondo = $novoservico[0]+1;
echo $valor_redondo;

He will always print the next number, but if it is int really:

$valor_redondo = intval($novoservico[0]+1);
  • Yes. But he’ll raise one. If I put 100 it goes to 2 instead of 1

  • If it’s 100, it’s 1 and it’s 9?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.