Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:44:17+00:00 2026-06-11T16:44:17+00:00

I am new to Grails. I would like to create a reusable function that

  • 0

I am new to Grails. I would like to create a reusable function that can calculate a percentage (0 – 100%) based on any 2 input values I specify. I would like this to be reusable across domains and controllers, but I am having a hard time figuring out where to put this function.

Here is my code:

def calcPercentComplete(hoursComp, hoursReq) {
  def dividedVal = hoursComp/hoursReq
  def Integer result = dividedVal * 100

  // results will have a min and max range of 0 - 100.
  switch(result){
    case{result > 100}:
      result = 100
      break

    case {result <= 0}:
      result =  0
      break

    default: return result
  }

}

Does anyone have advice on best practices for implementing this?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:44:19+00:00Added an answer on June 11, 2026 at 4:44 pm

    If you write a class (say called TimeUtils.groovy) and put it in src/groovy/utils

    Then add something that does this as a static method:

    package utils
    
    class TimeUtils {
      static Integer calcPercentComplete(hoursComp, hoursReq) {
        Integer result = ( hoursComp / hoursReq ) * 100.0
        result < 0 ? 0 : result > 100 ? 100 : result
      }
    }
    

    You should then be able to call:

    def perc = utils.TimeUtils.calcPercentComplete( 8, 24 )
    

    From anywhere in your code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use new table component added to JasperReports 3.7.2 with grails
I'm new to Grails development and would like to set up a new project,
I create a new grails app and do grails install-plugin rest . App builds.
In Grails, one can 'bindData' in controller: Book b = new Book() bindData(b, params)
I would like to use subreports with grails jasper plugins, I followed the manual
I have field int displayOrder = 1 in Grails domain object and would like
Using Grails and CXF, I have published a small web service that looks like
I have recently started reading up on Grails and would like to use SQL
I have an existing Grails project that I am upgrading to a new version.
I am wanting to create a page that can load a property file into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.