php function - Working with coin values

Asked

Viewed 393 times

2

Good evening , I’d like your help in solving a problem. Researching a little here I ended up finding a code that will help me at work with a page for spending and income registration.

I would like to ensure that this code does not round up the figures. when I type 0.3 cents it returns 1. If I type 0.030 it returns 1 too and so on. I wish I could work with the right decimal places because otherwise the final values won’t match my grades.

    function format_amount($n, $n_decimals) {
    return ((floor($n) == round($n, $n_decimals)) ? number_format($n).'.00' : number_format($n, $n_decimals));
}

Thanks in advance.

Source: https://github.com/santanumajumdar/MoneyManager/blob/master/includes/Functions.php

1 answer

1

The function round() is almost identical to function number_format(). To difference is that the second adds zeros to the right of the number if the value of the second parameter is greater than the number of boxes decimals of the first parameter. If the second parameter is omitted or is equal to 0, the number shall be rounded to the next integer if the decimal is equal to or greater than .5, or to the above, if the decimal is less than .5.

Exs.:

round(0.3,2); retorna 0.3
number_format(0.3,2); retorna 0.30
round(0.35); retorna 0
number_format(0.35); retorna 0
round(0.55); retorna 1
number_format(0.51); retorna 1

First suggestion is to remove from function format_amount($n, $n_decimals) the parole within the return, which becomes unnecessary, leaving only return number_format($n, $n_decimals);, in this way:

function format_amount($n, $n_decimals) {
    return number_format($n, $n_decimals);
}

The second suggestion is never send $n_decimals as 0 or less than the number of decimal places of the number in $n. If this is not observed, there is a risk of rounding. See:

format_amount('0.3', '1') // ok! 1 casa decimal e 1. Irá retornar 0.3
format_amount('0.3', '2') // ok! 1 casa decimal e 2. Irá retornar 0.30

format_amount('0.35', '1') // errado! 2 casas decimais e 1. Irá retornar 0.4
format_amount('0.353', '2') // errado! 3 casas decimais e 2. Irá retornar 0.35
format_amount(0.353,0); // errado! 3 casas decimais e 0. Irá retornar 0 (igual a round)
format_amount(0.553,0); // errado! 3 casas decimais e 0. Irá retornar 1 (igual a round)
format_amount(0.557,2); // errado! 3 casas decimais e 2. Irá retornar 0.56 (igual a round)
  • This issue of format_amount would be used as ? I was confused by it. I won’t deny it. And thanks for the first tip.

  • @Rafsq The format_amount takes 2 parameters, right? The first is the number, the second is the number of decimals you want to handle. If you send in the second parameter a number smaller than the number of decimal places sent in the first parameter, there may be rounding. For example. , if I send format_amount('3,5','2'), it will return "3,50", because I informed in the second parameter that I want 2 decimal places... if I send format_amount('3,55','1'), I am stating that I only want 1 decimal place, then in this case it will return "3,6"because the last decimal number is 5...

  • What I didn’t understand @Ķvý, is how to use this function ai. I’m not sure how to configure this function so that it returns the value as you explained. I’m "getting" it in the way how to use it inside what is already created.

  • @Rafsq What values will you send to the function? Can they vary? If so, I can rewrite the function so that it can return any value you send without arrendondamentos.

  • I have an input, a form where that value is placed. At the moment I am filling the values it only starts to put the comma after the 4 digit.Example : 1,111. If it’s only 111 it gets 111 as it was typed without adding the ,00. This function will be executed on top of this input and pass the value to the bank. That way you explained me I return values according to what I type in the field. About the values: If I type 0.30 it returns 1 .

  • @Rafsq I will redo the function to receive only 1 parameter, the number, and return the decimals automatically with 3 digits

  • Okay, @Ḍvý I’m here studying and researching to understand all this.

  • @Rafsq created a function where you only send the numbers and it returns with the decimals without rounding. See: https://ideone.com/X9bBmn

  • Thank you very much again @? But here’s the deal. It still doesn’t work. On the page I launch a value (e.g., expense page) when I type 0.30 instead of 0.30 it accepts the entered value but rounds. I keep not getting even after help. And once again, moral thank you.

  • @Rafsq You’re welcome! Consider checking if the answer was helpful. Abs!

Show 5 more comments

Browser other questions tagged

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