• Resolved s1s2

    (@s1s2)


    I have a post-type called “accounts” with an ACF numeric field called “value” I use to store a certain amount in different currencies (let’s say USD, GBP, CAD).

    I want to convert all of them in USD and sum them.

    To convert them I use another plugin called Open Currency Converter. It requires the following syntax: [convert number=123 from="gbp" to="usd"]

    The number that is stored in the “value” field has no separator for thousands. Example: 1246.8074 (this is one thousands, two hundreds, fortysix).

    The number returned by the converter plugin uses , to show the thousands:
    Example: 1,246.8074 (this still is one thousands, two hundreds, fortysix).

    When I try to add the “value” amount to the converted amount, the latter is considered in a completely different way:

    [pass vars]
    [calc][field value] + [convert number={VALUE} from="gbp" to="usd"][/calc]
    [/pass]

    If value = 0 and the converted value = 1,246.8074, I’d expect [calc] to return: 1,247.8074.

    Instead, [calc] returns: 1.

    Not only it wrongly assumes that a thousand is a single unit, but it also hides all decimals.

    I was thinking about using [format] to remove the , from the converted value, but I can’t find the right syntax to eliminate it.

    Am I missing something?

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter s1s2

    (@s1s2)

    This problem fundamentally boils down to the capability of stripping the comma from the conversion value generated by the Open Currency Converter plugin.

    Given that it’s not possible to customize the output Open Currency Converter, and given that I can’t figure out if and how Custom Content Shortcode can strip a comma, I opted for the following solution:

    1. I created a snippet with the Code Snippets plugin that looks like this:

    function format_my_number($atts) {
    $num = $atts["value"];return str_replace( ',', '', $num);
    }
    add_shortcode("strip-comma", "format_my_number");

    2. I passed the output of the Open Currency Converter plugin into this snippet via the shortcode:

    [set convertedtotal_USD][strip-comma value="{CONVERTED}"][/set]

    3. I calculated the sum of whatever I have to calculate.

    It’s not elegant, and I hope that there’s an easier way, but it works.

    You could have used contains to find the commas and format split to build you values.

    Thread Starter s1s2

    (@s1s2)

    I would like to explore that. Where is the syntax for format split? I couldn’t find it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Strange issue with [calc]’ is closed to new replies.