Calculate values (R$) from quantity

Asked

Viewed 775 times

0

I don’t know if the title explains the doubt well, but I’ll give you an example:

A product costs 50 cents each. I want customers to select the number of products they want to take. So far, so good.

If I multiply R$0.50xNumerPar, the result is an integer.

However, if I multiply by an odd number, the result is a broken number.

My code:

 if($mc_quantia == 1) {
     //Se a quantia for 1, o preço será 50 centavos                   
     $item = "R$0,50";
 } else {
     //Se não, pegue a quantia e divida por 2 (Porque cada 50 centavos é meio real, então eu preciso dividir por 2)
     $id_4 = $mc_quantia/2;

     $item = 'R$'.$id_4.',00';
 }

The problem:

If the amount is 3, will return R $ 1.50,00

I’d like you to return direct the $1.50

Can you help me??

  • 2

    Is mixing string formatting with numbers.

2 answers

2


You may not know it yet, but for this calculation there is a type of variable called float, which allows you to work with fractional numbers, without having to make this logic with the integer.

Try this:

 if($mc_quantia == 1) {
     $item = "R$0,50";
 } else {
     $id_4 = $mc_quantia*0.5;
     $item = 'R$'.number_format($id_4, 2, ',', '.');
 }

this code above would solve your problem, below follows the ideal, since the result is the same:

//O if/else é totalmente desnecessário, visto que mesmo sendo a quantidade 1,
//irá retornar o valor calculado corretamente
$id_4 = $mc_quantia*0.5;
$item = 'R$'.number_format($id_4, 2, ',', '.');

OBS. According to documentation officer, this function accepts only 1, 2 or 4 parameters, not 3

  • What version of php are you using? I tried it and it didn’t work, it gave error Wrong parameter count for number_format(). I had to put it like this: $item = 'R$'.number_format($id_4, 2, ',', '');

  • Strange, because independent of the version only the first parameter is required.

  • I could tell you the version?

  • I updated the answer...

  • I already knew the double and the float, but I didn’t know what the difference was or what each one was for. Anyway, it was worth.

  • Now yes. I’ll give upvote.

  • the last argument can be any string, it only needs to be passed as parameter, as I edited in the answer, had put wrong, because 3 parameters are not possible

  • Of course, as I wrote before ;)

  • And about Else and If, I know it wasn’t even necessary, but since I didn’t know the float business, I did this trick right there to return.

Show 4 more comments

1

You can simplify the code a little more:

$valor_unidade = 0.50;

$item = "R$" . number_format($valor_unidade * $mc_quantia);

And as in number_format documentation, you can format the variable as you would like:

<?php

$number = 1234.56;

// Notação Inglesa (padrão)
$english_format_number = number_format($number);
// 1,234

// Notação Francesa
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// Notação Inglesa com separador de milhar
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

Browser other questions tagged

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