Phate6660 / bcalc

A pure-bash calculator in which I even wrote my own lexer and parser for.
Other
5 stars 1 forks source link

Option for custom number formatting #8

Open adnan360 opened 3 years ago

adnan360 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

adnan360 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

adnan360 commented 3 years ago

I've checked it. It has these issues:

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

This will probably get complex.

Phate6660 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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