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

Option for custom number formatting #8

Open
adnan360 opened this issue Mar 26, 2021 · 11 comments
Open

Option for custom number formatting #8

adnan360 opened this issue Mar 26, 2021 · 11 comments

Comments

@adnan360
Copy link

I was thinking about the default comma formatting. Right now it does this:

$ ./bcalc '2*1000000000'
2,000,000,000

But there are other countries with other number formatting traditions. For example I found this article showing some of them. It would be great if this formatting can be specified.

I'm not sure what would be the best way to do it. I was hoping if it was possible to specify the format with an arg like ###,###,###.##. Another way would probably be to mention the country codes.

@Phate6660
Copy link
Owner

Phate6660 commented Mar 26, 2021

About that, printf should automatically format the output accordingly when LC_NUMERIC is set.
Though now that I think about it, it's probably not something commonly set.
Would a good solution to this be that the value for LC_NUMERIC can be passed as a 3rd argument?
And I was thinking that if no locale was specified, it would default to no formatting.

@Phate6660
Copy link
Owner

Just wanted to let you know I've started working on it here: https://github.com/Phate6660/bcalc/tree/output-formatting

@adnan360
Copy link
Author

Would a good solution to this be that the value for LC_NUMERIC can be passed as a 3rd argument?

I think that's a good start. Although I think being able to set format as ###,###.## would be nicer. Another problem would be if some locale is not setup on the system.

For example, on a minimal Void Linux system and it returns:

$ locale -a
C
en_US.utf8
POSIX

Setting any value for any other language doesn't work for me. e.g.

$ LC_NUMERIC='it_IT.UTF-8' && printf "%'.3f\n" 1234567.891
bash: warning: setlocale: LC_NUMERIC: cannot change locale (it_IT.UTF-8)
1,234,567.891

@Phate6660
Copy link
Owner

Oh hmm. builtin printf doesn't support locale stuff, but the actual command (not builtin) does.

Yep, I will definitely set up formatting in that case.

@Phate6660
Copy link
Owner

How does it look now? I just pushed a few commits to the branch.

@adnan360
Copy link
Author

I've checked it. It has these issues:

English Grouping 123,456,789.01
Indian Grouping 12,34,56,789.01

If we want to do this with args, we'd have to use 3 args:

  • decimal char
  • grouping char
  • group size (with support for variable size, such as Indian style)

This will probably get complex.

@Phate6660
Copy link
Owner

Phate6660 commented Mar 28, 2021

Well, I don't think we have to worry about decimals, since they aren't even supported im bash anyway. And adding support for them would lead to this not being pure bash. That being said, it should be simple to allow the user to change how the numbers are grouped.

@Phate6660
Copy link
Owner

Phate6660 commented Mar 28, 2021

If you check the article, some countries have comma as decimal symbol and period as grouping symbol. e.g. Argentina

This is already supported in the output-format branch. Supply , or . as an arg.

@adnan360
Copy link
Author

adnan360 commented Mar 28, 2021

Well, I don't think we have to worry about decimals, since they aren't even supported im bash anyway. And adding support for them would lead to this not being pure bash.

Oh, I didn't know that. Can the decimal places calculation be done manually? Like we do in paper?

Until it's possible the current implementation seems fine for a bash calculator. But I'm curious if we would need other more important args to be the second one later in time. Maybe we should specify it with an identifier, like -g , or --grouping-char=,?

Sorry if I'm pushing things far. You don't have to consider these if these are too complex for the scope of the project.

That being said, it should be simple to allow the user to change how the numbers are grouped.

That would be great thanks.

EDIT: Added longer version of arg

@Phate6660
Copy link
Owner

Phate6660 commented Mar 28, 2021

Oh, I didn't know that. Can the decimal places calculation be done manually? Like we do in paper?

Oh it definitely could, thinking about it now.
I will work on adding support for that in a separate branch called decimals
once I finish working on output-formatting.

But I'm curious if we would need other more important args to be the second one later in time. Maybe we should specify it with an identifier, like -g , or --grouping-char=,?

That is certainly a possiblity.
For now, I think it will be fine to just make sure the args documented in the README.
But once this starts becoming even more fleshed out, I have no doubt that more args will be needed.
So when the time comes, I will definitely start making identifiers for the args.

Sorry if I'm pushing things far. You don't have to consider these if these are too complex for the scope of the project.

On no, don't be! I'm glad to have these things asked for.
Not only does it give me something to do, but figuring out how to solve the question at hand can be fun.
Usually when I script/program, it just sort of gets thrown into the void of the internet.
So it's nice to have people using my stuff and requesting things to be added.

And as a P.S., I'm really close to getting the custom grouping done.
I just need to modify and add a few calculations to make it place the grouping character properly.

@adnan360
Copy link
Author

That's great to hear. Looking forward to it. Let me know if I can test anything.

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

No branches or pull requests

2 participants