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

HumanFloatCount for per_sec #453

Merged
merged 7 commits into from
Aug 2, 2022

Conversation

jqnatividad
Copy link
Contributor

@djc here's my attempt to resolve #392.

HumanFloatCount largely mimics HumanCount with the exception that it accepts f64, and truncates the fractional part at 4 decimal places. If the float is a whole number, it doesn't add ".0" to the end.

    #[test]
    fn human_float_count() {
        assert_eq!("42", format!("{}", HumanFloatCount(42.0)));
        assert_eq!("7,654", format!("{}", HumanFloatCount(7654.0)));
        assert_eq!("12,345", format!("{}", HumanFloatCount(12345.0)));
        assert_eq!(
            "1,234,567,890",
            format!("{}", HumanFloatCount(1234567890.0))
        );
        assert_eq!("42.5", format!("{}", HumanFloatCount(42.5)));
        assert_eq!("7,654.321", format!("{}", HumanFloatCount(7654.321)));
        assert_eq!("12,345.6789", format!("{}", HumanFloatCount(12345.6789)));
        assert_eq!(
            "1,234,567,890.1234",
            format!("{}", HumanFloatCount(1234567890.123456))
        );
    }

per_sec uses it, so instead of

210879.6613/s, you'd get 210,879.6613/s

@jqnatividad jqnatividad marked this pull request as draft July 31, 2022 12:54
@jqnatividad
Copy link
Contributor Author

jqnatividad commented Jul 31, 2022

now also rounds to four decimal places and trims trailing zeroes

    #[test]
    fn human_float_count() {
        assert_eq!("42", format!("{}", HumanFloatCount(42.0)));
        assert_eq!("7,654", format!("{}", HumanFloatCount(7654.0)));
        assert_eq!("12,345", format!("{}", HumanFloatCount(12345.0)));
        assert_eq!(
            "1,234,567,890",
            format!("{}", HumanFloatCount(1234567890.0))
        );
        assert_eq!("42.5", format!("{}", HumanFloatCount(42.5)));
        assert_eq!("42.5", format!("{}", HumanFloatCount(42.500012345)));
        assert_eq!("42.502", format!("{}", HumanFloatCount(42.502012345)));
        assert_eq!("7,654.321", format!("{}", HumanFloatCount(7654.321)));
        assert_eq!("7,654.321", format!("{}", HumanFloatCount(7654.3210123456)));
        assert_eq!("12,345.6789", format!("{}", HumanFloatCount(12345.6789)));
        assert_eq!(
            "1,234,567,890.1235",
            format!("{}", HumanFloatCount(1234567890.1234567))
        );
        assert_eq!(
            "1,234,567,890.1234",
            format!("{}", HumanFloatCount(1234567890.1234321))
        );
    }

@jqnatividad jqnatividad marked this pull request as ready for review July 31, 2022 14:13
@djc djc merged commit 989321a into console-rs:main Aug 2, 2022
@djc
Copy link
Member

djc commented Aug 2, 2022

Thanks!

@jqnatividad jqnatividad deleted the human_float_count-per_sec branch August 3, 2022 16:20
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.

HumanCount formatting for per_sec?
2 participants