Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Is there a way to only show integer percentage but not float value?

This discussion is related to the Discussion Polls addon.

Hello,
At the moment it shows float percentage value , like 100.00% for vote counts. Is it possible not to include values after decimal , like 100% ?

Thank you in advance

Comments

  • RiverRiver MVP
    edited October 2016

    @vanillawhisky said:
    Hello,
    At the moment it shows float percentage value , like 100.00% for vote counts. Is it possible not to include values after decimal , like 100% ?

    Thank you in advance

    Do you see this line?

    https://github.com/hgtonight/Plugin-DiscussionPolls/blob/master/views/results.php#L45

    $Percentage = ($Question->CountResponses == 0) ? '0.00' : number_format(($Option->CountVotes / $Question->CountResponses * 100), 2);

    the 2 at the end stands for number of decimal places.

    you would also probably want to change 0.00 to 0

    http://php.net/manual/en/function.number-format.php

    the second parameter controls the numbers beyond the decimal point. You can remove the second argument.

    see how the removal of the , 2 removes the decimal point and the 2 numbers following the decimal point.

    http://www.w3schools.com/php/showphp.asp?filename=demo_func_string_number_format

    http://www.w3resource.com/php/function-reference/number_format.php

    you could replace the view.

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

    vanillawhiskyhgtonight
  • @River said:

    https://github.com/hgtonight/Plugin-DiscussionPolls/blob/master/views/results.php#L45

    $Percentage = ($Question->CountResponses == 0) ? '0.00' : number_format(($Option->CountVotes / $Question->CountResponses * 100), 2);

    Thanks for your comment.
    I tried to rewrite the function

    function RenderQuestion($Question)

    and put in themehook file but (i guess) since this is not a public function, this doesnt work.
    Is there a way to do without directly changing in plugins itself?

    Thank you!!!

  • LincLinc Vanilla's Bard (and Director of Development) Detroit Vanilla Staff
    edited October 2016

    I'd either ask the plugin author to change or allow for it, or consider modifying it via Javascript after it's output.

    It cannot be overridden as currently implemented.

  • RiverRiver MVP
    edited October 2016

    @vanillawhisky said:

    @River said:

    https://github.com/hgtonight/Plugin-DiscussionPolls/blob/master/views/results.php#L45

    $Percentage = ($Question->CountResponses == 0) ? '0.00' : number_format(($Option->CountVotes / $Question->CountResponses * 100), 2);

    Thanks for your comment.
    I tried to rewrite the function

    function RenderQuestion($Question)

    and put in themehook file but (i guess) since this is not a public function, this doesnt work.
    Is there a way to do without directly changing in plugins itself?

    Yes.

    Thank you!!!

    like I said you could change the VIEW for this particular plugin if you don't want to modify the plugin itself. This is not a general technique for all plugins.

    changing the view "hiding" or overriding changes in the theme folder or changing a bootstrap function can get you in as much trouble or more than just forking and changing the darn plugin. The view or function can be stale, a security flaw may be fixed and you will happily upgrade your plugin without a worry, because you thought since you didn't directly modify the plugin you are home free. Point being, know what you are doing, keep it simple. Sometimes hundreds of code gyrations to keep a community plugin unchanged is a bit overdone in my mind and prone to just as much problems as directly making a simple change and documenting it for the other admins.
    or propose a change via a github to just have a changeable variable thru translation or config for the second argument in the percentage code for the next update of the plugin.

    • view override

    copy

    /plugins/DiscussionPolls/views/results.php to ....

    lets say your theme is called bittersweet

    /themes/bittersweet/views/plugins/DiscussionPolls/results.php

    make the change to the results.php in bittersweet/views/plugins/DiscussionPolls/

    change

    $Percentage = ($Question->CountResponses == 0) ? '0.00' : number_format(($Option->CountVotes / $Question->CountResponses * 100), 2);
    

    to

    either

    $Percentage = ($Question->CountResponses == 0) ? '0' : number_format(($Option->CountVotes / $Question->CountResponses * 100), 0);
    

    or

    $Percentage = ($Question->CountResponses == 0) ? '0' : number_format(($Option->CountVotes / $Question->CountResponses * 100));
    

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

    vanillawhisky
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    I love to say that: it can be done in this case! image

    This is the place where the view "results.php" is included:

      /**
       * Renders a poll object as results
       * @param stdClass $Poll the poll object we are rendering
       * @param boolean $Echo echo or return result string
       * @return mixed Will return string if $Echo is false, will return true otherwise
       */
      protected function _RenderResults($Poll, $Echo = TRUE) {
        include($this->ThemeView('results'));
    

    But it is not simply included, it uses a helper function instead:

      /*
       * Set view that can be copied over to current theme
       * e.g. view -> current_theme/views/plugins/DiscussionPolls/view.php
       * @param View name of the view
       */
    
      public function ThemeView($View) {
    

    You can see in the functions documentation how you can override the view. You need a custom theme for that, though.

    All credits go to @x00 https://vanillaforums.org/discussion/comment/225740#Comment_225740

  • RiverRiver MVP
    edited October 2016

    @R_J - I provided the complete step-by-step solution. thanks for echoing an abridged version :wink:

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Oops! Last time I've loaded the discussion there have been no answers.

    But at least it has given the credits to the correct person.

  • RiverRiver MVP
    edited October 2016

    @R_J said:
    Oops! Last time I've loaded the discussion there have been no answers.

    I've tried to save my draft and reload the discussion prior to posting to prevent that type of mishap. but it can happen.

    But at least it has given the credits to the correct person.

    yes nice idea by x00 otherwise the snippet would have probably never have found its way into the author's plugin code.

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • @River and @R_J
    Thanks guys, it works.
    and thank you everyone for help.
    u guys rocks.

Sign In or Register to comment.