Gast Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 Ik wil een bedrag afronden in PHP op cent. Dus eindbedrag moet eindigen op een 0 of op een 5.(vb 6,80 / 6,85) Met de functie round() lukt mij dat niet. Wie weet raad? <img src="/forums/images/graemlins/wink.gif" alt="" />
Duwgati Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 <?php function roundIt( $val = null ) { return( round( $val * 20, 0 ) / 20 ); } print roundIt( $bedrag ); ?> Zoiets?
Gast Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 Ik ben er nog niet uit. Het is een script uit een calculatieprogramma wat verder prima werkt. Alleen de afrondingen lukken niet. Het script wat ik gebruik zie hieronder. ?> function doRound(x, places) { return Math.round(x * Math.pow(10, places)) / Math.pow(10, places); } function getTaxRate() { var selected_value = document.forms["new_product"].products_tax_class_id.selectedIndex; var parameterVal = document.forms["new_product"].products_tax_class_id[selected_value].value; if ( (parameterVal > 0) && (tax_rates[parameterVal] > 0) ) { return tax_rates[parameterVal]; } else { return 0; } } function updateGross() { var taxRate = getTaxRate(); var grossValue = document.forms["new_product"].products_price.value; if (taxRate > 0) { grossValue = grossValue * ((taxRate / 100) + 1); } document.forms["new_product"].products_price_gross.value = doRound(grossValue, 4); } function updateNet() { var taxRate = getTaxRate(); var netValue = document.forms["new_product"].products_price_gross.value; if (taxRate > 0) { netValue = netValue / ((taxRate / 100) + 1); } document.forms["new_product"].products_price.value = doRound(netValue, 4); } function updateCost() { var costValue = document.forms["new_product"].products_cost.value; document.forms["new_product"].products_cost.value = doRound(costValue,4); } function updateMarkup() { var markupValue = document.forms["new_product"].products_markup.value; var costValue = document.forms["new_product"].products_cost.value; var costValue = Number(costValue); var markupValue = Number(markupValue); var markup = costValue + ((costValue * markupValue)/100); document.forms["new_product"].products_price.value = doRound(markup,4); } function updatemarginGrossDollar() { var costValue = document.forms["new_product"].products_cost.value; var netValue = document.forms["new_product"].products_price.value var costValue = Number(costValue); var netValue = Number(netValue); var marginGrossDollar = netValue - costValue; document.forms["new_product"].products_margin_gross_dollar.value = doRound(marginGrossDollar,4); } function updatemarginGrossPercent() { var marginGrossDollarValue = document.forms["new_product"].products_margin_gross_dollar.value; var netValue = document.forms["new_product"].products_price.value var marginGrossDollarValue = Number(marginGrossDollarValue); var netValue = Number(netValue); var marginGrossPercent = (marginGrossDollarValue / netValue) * 100; document.forms["new_product"].products_margin_gross_percent.value = doRound(marginGrossPercent,4); } //--></script>
Duwgati Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 Dat is geen PHP, dat is javascript. En die function doRound(x, places) is degene waar je de wijzigingen moet aanbrengen.
Gast Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 Sorry, je hebt volkomen gelijk. het is javascript. Ik ben er zo verwoed mee bezig dat ik wartaal begin uit te slaan. Ik weet dat het in die functie zit. Alleen lukt het mij nog steeds niet.
Big fellow Geplaatst: 9 april 2007 Geplaatst: 9 april 2007 Analoog aan "1 + 1 = 2"; function roundIt( $val = null ) { return( round( $val * 20, 0 ) / 20 ); } + function doRound(x, places) { return Math.round(x * Math.pow(10, places)) / Math.pow(10, places); } = function doRound(x, places) { return Math.round((x * 0.2 * Math.pow(10, places)) / (0.2 * Math.pow(10, places))); } Dit is niet getest, alleen maar een combinatie van beide... Big fellow “Success is not final, failure is not fatal: it is the courage to continue that counts.” ~ Winston Churchill
Gast Geplaatst: 11 april 2007 Geplaatst: 11 april 2007 Er wordt nu afgerond op hele getallen. Ik wil graag afronden op 5 cent. Dus als eindgetal bv. 7,20 of 7,15, niet op 7. <img src="/forums/images/graemlins/smirk.gif" alt="" />
Big fellow Geplaatst: 11 april 2007 Geplaatst: 11 april 2007 Probeer deze "versie" eens; function doRound(x, places) { return Math.round(Math.round((x * 0.2 * Math.pow(10, places))) / (0.2 * Math.pow(10, places))); } Werkt hij dan wel goed? Big fellow “Success is not final, failure is not fatal: it is the courage to continue that counts.” ~ Winston Churchill
Big fellow Geplaatst: 11 april 2007 Geplaatst: 11 april 2007 Ik zie al waar de fout zit.... Beetje over het hoofd gezien. Haal de eerste Math.round weg. Dus de functie wordt dan: function doRound(x, places) { return Math.round((x * 0.2 * Math.pow(10, places))) / (0.2 * Math.pow(10, places)); } Als je dan namelijk de functie uitschrijft en analyseert, dan moet hij werken.... Je krijgt dan namelijk bij 7.12 en 2: Math.round((7.12 * 0.2 * 10^2))/(0.2 * 10^2)) of ook: Math.round((7.12 * 20))/(20) Als je die uitrekent kom je op: Math.round((142.4))/(20) wat dus 142/20 wordt, welke als uitkomst 7.1 geeft ....... Big fellow “Success is not final, failure is not fatal: it is the courage to continue that counts.” ~ Winston Churchill
Gast Geplaatst: 12 april 2007 Geplaatst: 12 april 2007 Dit werkt. Reuze bedankt. Ik heb weer bijgeleerd. <img src="/forums/images/graemlins/laugh.gif" alt="" />
Aanbevolen berichten
Maak een account aan of log in om te reageren
Je moet een lid zijn om een reactie te kunnen achterlaten
Account aanmaken
Registreer voor een nieuwe account in onze community. Het is erg gemakkelijk!
Registreer een nieuwe accountInloggen
Heb je reeds een account? Log hier in.
Nu inloggen