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 magentoMagento displaying base currency not default display currencyHow to add a custom price attribute for RRP?Pricing PrecisionMagento2 : Problem in block creationCurrency ConverterError Database Magento 2.3 migration from localhost to server

Chemistry Riddle

Found old paper shares of Motorola Inc that has since been broken up

Can we have too many dialogue tags and follow up actions?

How could Barty Crouch Jr. have run out of Polyjuice Potion at the end of the Goblet of Fire movie?

Is an easily guessed plot twist a good plot twist?

Impact of throwing away fruit waste on a peak > 3200 m above a glacier

What is a plausible power source to indefinitely sustain a space station?

Why is there an extra "t" in Lemmatization?

What kind of world would drive brains to evolve high-throughput sensory?

Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?

How can Kazakhstan perform MITM attacks on all HTTPS traffic?

Killing a star safely

Can you find Airpod Case using Find my iPhone?

How did pilots avoid thunderstorms and related weather before “reliable” airborne weather radar was introduced on airliners?

Character Arcs - What if the character doesn't overcome the big lie, flaws or wounds?

how to add 1 milliseconds on a datetime string?

Extrapolation v. Interpolation

How can I indicate that what I'm saying is not sarcastic online?

Are there any English words pronounced with sounds/syllables that aren't part of the spelling?

What's the 1 inch size square knob sticking out of wall?

If hash functions append the length, why does length extension attack work?

What kind of curve (or model) should I fit to my percentage data?

Progressive key bindings

How to work a regular job as a former celebrity



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 magentoMagento displaying base currency not default display currencyHow to add a custom price attribute for RRP?Pricing PrecisionMagento2 : Problem in block creationCurrency ConverterError Database Magento 2.3 migration from localhost to server






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom: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
























  • 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
























  • 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

1341 silver badge4 bronze badges




1341 silver badge4 bronze badges












  • 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

        1341 silver badge4 bronze badges




        1341 silver badge4 bronze badges



























            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

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?