Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for float in oneof - issue #1105 #1116

Closed
wants to merge 17 commits into from
Closed

added support for float in oneof - issue #1105 #1116

wants to merge 17 commits into from

Conversation

alirezakazemeini
Copy link

Based on the given issue, the func isOneOf(fl FieldLevel) function handles various data types including strings, integers, and floats. However, it seems like there is an issue with floats containing fractional parts.

To modify the code and make it support float values without fractions, you can update the decimal2String function to round the float value instead of throwing a panic when there is a fractional part. Here's an updated version of the function:

func decimal2String(f float64) string {
	rounded := math.Round(f)
	return strconv.FormatFloat(rounded, 'f', 0, 64)
}

or you can use it inline like:

v = strconv.FormatFloat(math.Round(field.Float()), 'f', 0, 64)

By using the math.Round function, the float value will be rounded to the nearest integer. This way, when converting the float to a string, you'll have the desired behavior of supporting float values without fractional parts.

@alirezakazemeini alirezakazemeini requested a review from a team as a code owner June 15, 2023 05:31
@coveralls
Copy link

coveralls commented Jun 15, 2023

Coverage Status

coverage: 73.975% (+0.003%) from 73.972% when pulling db53d02 on alirezakazemeini:issue-1105-oneof_float_support into bd1113d on go-playground:master.

@alirezakazemeini alirezakazemeini closed this by deleting the head repository Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants