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

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

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

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form