fix double precision problem

This commit is contained in:
Chris Cromer 2019-01-16 12:22:02 -03:00
parent 346be0e9ac
commit a0d1af51c4
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
2 changed files with 4 additions and 1 deletions

View File

@ -55,7 +55,8 @@ namespace LibSernatur {
* @return Returns a string of the percentage
*/
public static string format_double (double value) {
return (value * 100).to_string () + "%";
// Remove the double precision by converting to float
return ((float) (value * (double) 100)).to_string () + "%";
}
}

View File

@ -2,8 +2,10 @@ using LibSernatur.Misc;
void add_percentage_tests () {
Test.add_func ("/sernatur/test/percentage", () => {
assert (Percentage.format_float ((float) 0.07) == "7%");
assert (Percentage.format_float ((float) 0.43) == "43%");
assert (Percentage.format_float ((float) 1) == "100%");
assert (Percentage.format_double (0.07) == "7%");
assert (Percentage.format_double (0.43) == "43%");
assert (Percentage.format_double (1) == "100%");
});