I'm using a float to hold a score. The score can be an integer or decimal. By default, floats display as 0.0, 1.0, etc. If the number does not have a decimal, I need it to display as 0, 1, etc. If it does have a decimal, then I need to display the decimal. How might I do this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Your best bet is to work out the smallest granularity of score and then use that with an appropriate multiplier.
For example, if the smallest increment is 0.01, use a multiplier of 100. And if your score % mulitplier = 0 then you know its a whole number.
That way you dont need to worry about rounding, or representation errors.
Warning: Untested code. ;)Ok, I've tested it and fixed an error.
You could use:
to display with decimal places when present.
To counter against rounding errors,
score
here could be represented using a BigDecimal.