Skip to main content

Rounding Computation Documentation

This code provides functions to round numbers based on specific rounding settings. The settings are defined in a cache and can be used to round various types of values such as shift times, durations, rest periods, and overtime.

Constants

const (
// Type of setting
TYPE_SHIFT = "shift"
TYPE_DURATION = "duration"
TYPE_REST = "rest"
TYPE_OVERTIME = "overtime"

// Label of setting
LABEL_LATE = "late"
LABEL_INTIME = "in_time"
LABEL_GO_HOME_LATE = "go_home_late"
LABEL_GO_HOME_EARLY = "go_home_early"
LABEL_DURATION = "duration"
LABEL_OVER_REST = "over_rest"
LABEL_REST_DURATION = "rest_duration"
LABEL_EARLY_OVERTIME = "early_overtime"
LABEL_REST_OVERTIME = "rest_overtime"
LABEL_LATE_OVERTIME = "late_overtime"
LABEL_SHIFT_OVERTIME = "shift_overtime"
LABEL_TOTAL_OVERTIME = "total_overtime"

// Round of setting
ROUND_UP = "up"
ROUND_DOWN = "down"
ROUND_DEFAULT = "default"
ROUND_NONE = "none"
)

RoundNumberUseSetting Function

Function Signature

func RoundNumberUseSetting(num float64, label string, cache map[string]*entity.RoundingSettingTable) float64

Parameters

  • num float64: The number to be rounded.
  • label string: The label of the rounding setting to be used.
  • cache map[string]*entity.RoundingSettingTable: A cache containing the rounding settings.

Description

The RoundNumberUseSetting function rounds a number based on the specified rounding setting. The rounding setting is retrieved from the cache using the provided label. The function supports different rounding methods such as rounding up, rounding down, and rounding to the nearest value.

Steps

  1. Retrieve Setting: The function retrieves the rounding setting from the cache using the provided label.
  2. Determine Divider: The function determines the divider based on the TotalDecimal value in the setting.
  3. Apply Rounding: The function applies the rounding method specified in the setting (ROUND_DEFAULT, ROUND_UP, ROUND_DOWN).
  4. Return Result: The function returns the rounded number.

Example Usage

package main

import (
"fio-backend/entity"
"fio-backend/utils/compute"
"fmt"
)

func main() {
// Create a sample rounding setting
roundingSetting := &entity.RoundingSettingTable{
TotalDecimal: 2,
Round: compute.ROUND_DEFAULT,
}

// Create a cache with the rounding setting
cache := map[string]*entity.RoundingSettingTable{
compute.LABEL_DURATION: roundingSetting,
}

// Round a number using the rounding setting
num := 123.456789
roundedNum := compute.RoundNumberUseSetting(num, compute.LABEL_DURATION, cache)

// Print the rounded number
fmt.Printf("Original Number: %f, Rounded Number: %f\n", num, roundedNum)
}

Improvements and Considerations

  • Error Handling: Improve error handling by returning errors or using default values if the setting is not found in the cache.
  • Code Clarity and Comments: Improve code comments to explain complex logic more clearly.
  • Unit Tests: Add unit tests to ensure the function works correctly with various inputs and edge cases.
  • Performance Optimization: Optimize the function for performance, especially when processing large numbers of values.
  • Modularization: Break down the function into smaller, more modular functions to improve readability and maintainability.

This comprehensive documentation provides a detailed understanding of the rounding computation function and its functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for rounding numbers based on specific settings.