Magento 2 - Rounding Float Values to Currency PrecisionChange currency precisionDynamic Price with currency which differs from base currencyRounding products prices in Magento 1.8 CEcurrency position magentoCost price how to change currency?Magento displaying base currency not default display currencyChange currency precision in Magento 2 Product detail pagePricing PrecisionMagento 2: Plugin class does not existCurrency Converter

Do I need life insurance if I can cover my own funeral costs?

How to simplify this time periods definition interface?

How do I hide Chekhov's Gun?

Is a lawful good "antagonist" effective?

Why is Vishnu's skin colour mentioned as both white and blue in the Vishnu-sahasranaama?

Welcoming 2019 Pi day: How to draw the letter π?

What is the greatest age difference between a married couple in Tanach?

An Accountant Seeks the Help of a Mathematician

Variant calling without matched normal sample

Cultural lunch issues

Identifying the interval from A♭ to D♯

Bastion server: use TCP forwarding VS placing private key on server

Can anyone tell me why this program fails?

Have researchers managed to "reverse time"? If so, what does that mean for physics?

SQL Server Primary Login Restrictions

My adviser wants to be the first author

Implicit nil checks in algorithms

Specifying author pseudonyms in BibLaTeX

Sword in the Stone story where the sword was held in place by electromagnets

What to do when during a meeting client's people start to (physically) fight with each other?

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

No, nay, never, no more

Plot a function of two variables equal 0

What has been your most complicated TikZ drawing?



Magento 2 - Rounding Float Values to Currency Precision


Change currency precisionDynamic Price with currency which differs from base currencyRounding products prices in Magento 1.8 CEcurrency position magentoCost price how to change currency?Magento displaying base currency not default display currencyChange currency precision in Magento 2 Product detail pagePricing PrecisionMagento 2: Plugin class does not existCurrency Converter













0















Currently I have stored values based on percentages of order subtotals out to a precision of 4 decimal places in the database. I need to show a balance as a total sum of those percentages. I have an account with two calculated percentage totals, both equaling 2.8430. So to create a display price I add them up 2.8430 + 2.8430 = 5.6860 and send that value to the toCurrency() function of the MagentoFrameworkLocaleCurrencyInterface class.



<?php

/*
* @var $store MagentoStoreModelStoreManagerInterface
* @var $currency MagentoFrameworkLocaleCurrencyInterface
*/
$store = $this->storeManager->getStore();
$currency = $this->currency->getCurrency($store->getBaseCurrencyCode());
$value = $currency->toCurrency(sprintf("%f", $value), $options);


However, that's displaying $5.69 when it should be displaying $5.68 in currencies with a precision of 2. I can't find a method in the currency class that will round numbers based on currency precision and I need to do that in order to display the price correctly as well as run some conditional checks on the values to make sure customer requests for withdrawals aren't greater than the sum (5.68).



I've resorted to sending the values to the toCurrency() function, removing the symbol, and running the string result through floatval(), but I feel like this is a hack and I'm wondering if there's a more appropriate way?



$currencyPrecision = $currentAccount->convertToPrice($value, ['display' => Zend_Currency::NO_SYMBOL]);
$availableAmount = floatval($currencyPrecision);









share|improve this question
















bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

    – simonthesorcerer
    Feb 17 '18 at 20:19











  • Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

    – Nfourteen
    Feb 17 '18 at 20:24












  • can you show the code where the values get summed please?

    – simonthesorcerer
    Feb 17 '18 at 20:26











  • 2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

    – Nfourteen
    Feb 17 '18 at 20:47















0















Currently I have stored values based on percentages of order subtotals out to a precision of 4 decimal places in the database. I need to show a balance as a total sum of those percentages. I have an account with two calculated percentage totals, both equaling 2.8430. So to create a display price I add them up 2.8430 + 2.8430 = 5.6860 and send that value to the toCurrency() function of the MagentoFrameworkLocaleCurrencyInterface class.



<?php

/*
* @var $store MagentoStoreModelStoreManagerInterface
* @var $currency MagentoFrameworkLocaleCurrencyInterface
*/
$store = $this->storeManager->getStore();
$currency = $this->currency->getCurrency($store->getBaseCurrencyCode());
$value = $currency->toCurrency(sprintf("%f", $value), $options);


However, that's displaying $5.69 when it should be displaying $5.68 in currencies with a precision of 2. I can't find a method in the currency class that will round numbers based on currency precision and I need to do that in order to display the price correctly as well as run some conditional checks on the values to make sure customer requests for withdrawals aren't greater than the sum (5.68).



I've resorted to sending the values to the toCurrency() function, removing the symbol, and running the string result through floatval(), but I feel like this is a hack and I'm wondering if there's a more appropriate way?



$currencyPrecision = $currentAccount->convertToPrice($value, ['display' => Zend_Currency::NO_SYMBOL]);
$availableAmount = floatval($currencyPrecision);









share|improve this question
















bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

    – simonthesorcerer
    Feb 17 '18 at 20:19











  • Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

    – Nfourteen
    Feb 17 '18 at 20:24












  • can you show the code where the values get summed please?

    – simonthesorcerer
    Feb 17 '18 at 20:26











  • 2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

    – Nfourteen
    Feb 17 '18 at 20:47













0












0








0








Currently I have stored values based on percentages of order subtotals out to a precision of 4 decimal places in the database. I need to show a balance as a total sum of those percentages. I have an account with two calculated percentage totals, both equaling 2.8430. So to create a display price I add them up 2.8430 + 2.8430 = 5.6860 and send that value to the toCurrency() function of the MagentoFrameworkLocaleCurrencyInterface class.



<?php

/*
* @var $store MagentoStoreModelStoreManagerInterface
* @var $currency MagentoFrameworkLocaleCurrencyInterface
*/
$store = $this->storeManager->getStore();
$currency = $this->currency->getCurrency($store->getBaseCurrencyCode());
$value = $currency->toCurrency(sprintf("%f", $value), $options);


However, that's displaying $5.69 when it should be displaying $5.68 in currencies with a precision of 2. I can't find a method in the currency class that will round numbers based on currency precision and I need to do that in order to display the price correctly as well as run some conditional checks on the values to make sure customer requests for withdrawals aren't greater than the sum (5.68).



I've resorted to sending the values to the toCurrency() function, removing the symbol, and running the string result through floatval(), but I feel like this is a hack and I'm wondering if there's a more appropriate way?



$currencyPrecision = $currentAccount->convertToPrice($value, ['display' => Zend_Currency::NO_SYMBOL]);
$availableAmount = floatval($currencyPrecision);









share|improve this question
















Currently I have stored values based on percentages of order subtotals out to a precision of 4 decimal places in the database. I need to show a balance as a total sum of those percentages. I have an account with two calculated percentage totals, both equaling 2.8430. So to create a display price I add them up 2.8430 + 2.8430 = 5.6860 and send that value to the toCurrency() function of the MagentoFrameworkLocaleCurrencyInterface class.



<?php

/*
* @var $store MagentoStoreModelStoreManagerInterface
* @var $currency MagentoFrameworkLocaleCurrencyInterface
*/
$store = $this->storeManager->getStore();
$currency = $this->currency->getCurrency($store->getBaseCurrencyCode());
$value = $currency->toCurrency(sprintf("%f", $value), $options);


However, that's displaying $5.69 when it should be displaying $5.68 in currencies with a precision of 2. I can't find a method in the currency class that will round numbers based on currency precision and I need to do that in order to display the price correctly as well as run some conditional checks on the values to make sure customer requests for withdrawals aren't greater than the sum (5.68).



I've resorted to sending the values to the toCurrency() function, removing the symbol, and running the string result through floatval(), but I feel like this is a hack and I'm wondering if there's a more appropriate way?



$currencyPrecision = $currentAccount->convertToPrice($value, ['display' => Zend_Currency::NO_SYMBOL]);
$availableAmount = floatval($currencyPrecision);






magento2 price currency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 17 '18 at 20:20







Nfourteen

















asked Feb 17 '18 at 20:13









NfourteenNfourteen

13414




13414





bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 5 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

    – simonthesorcerer
    Feb 17 '18 at 20:19











  • Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

    – Nfourteen
    Feb 17 '18 at 20:24












  • can you show the code where the values get summed please?

    – simonthesorcerer
    Feb 17 '18 at 20:26











  • 2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

    – Nfourteen
    Feb 17 '18 at 20:47

















  • 5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

    – simonthesorcerer
    Feb 17 '18 at 20:19











  • Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

    – Nfourteen
    Feb 17 '18 at 20:24












  • can you show the code where the values get summed please?

    – simonthesorcerer
    Feb 17 '18 at 20:26











  • 2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

    – Nfourteen
    Feb 17 '18 at 20:47
















5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

– simonthesorcerer
Feb 17 '18 at 20:19





5.6860 is rounded to 5.69 - this is correct. To achive your desired result, imho you must round the two percentages before you calculate the final percentage. this would be 2.84 + 2.84 = 5.68

– simonthesorcerer
Feb 17 '18 at 20:19













Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

– Nfourteen
Feb 17 '18 at 20:24






Correct, but I need the sum, so how do I round those 2.843 to 2.84 based on the precision of the currency? Currently, I'm running them both through that last block, which gives me a string value, which I then convert back to a float via floatval().

– Nfourteen
Feb 17 '18 at 20:24














can you show the code where the values get summed please?

– simonthesorcerer
Feb 17 '18 at 20:26





can you show the code where the values get summed please?

– simonthesorcerer
Feb 17 '18 at 20:26













2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

– Nfourteen
Feb 17 '18 at 20:47





2.843 is stored in the database. So the values are just pulled from the rows of the database. I'm writing the function to sum them now for display, but I don't know how to round them based on the currency to avoid rounding display errors. I'm asking if there's a better way to round them than to send them to the toCurrency() method, implemented in Zend_Currency, with no symbol, get the string value back and then send that string through floatval().

– Nfourteen
Feb 17 '18 at 20:47










1 Answer
1






active

oldest

votes


















0














I found MagentoDirectoryModelCurrency, which I think will work.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f213939%2fmagento-2-rounding-float-values-to-currency-precision%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found MagentoDirectoryModelCurrency, which I think will work.






    share|improve this answer



























      0














      I found MagentoDirectoryModelCurrency, which I think will work.






      share|improve this answer

























        0












        0








        0







        I found MagentoDirectoryModelCurrency, which I think will work.






        share|improve this answer













        I found MagentoDirectoryModelCurrency, which I think will work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 17 '18 at 22:13









        NfourteenNfourteen

        13414




        13414



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f213939%2fmagento-2-rounding-float-values-to-currency-precision%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Masuk log Menu navigasi

            16 Maret Daftar isi Peristiwa | Kelahiran | Meninggal | Hari raya dan peringatan | Menu navigasis

            ジョン・ファウルズ