Right way to show numbers in binary/hex/octal in your opinion?
siarhei.siamashka at gmail.com
Sun Dec 26 17:35:12 UTC 2021
On Saturday, 25 December 2021 at 21:12:33 UTC, rempas wrote:
> Second method:
> 1. It is probably what people would expect and what makes
> scientifically more sense as decimal was supposed to be the
> only base that will make sense for humans to read hence be the
> only base that has the "-" character.
I don't think that this makes any sense. Numbers can be negative
regardless of the base that is used to show them. If "decimal was
supposed to be the only base that will make sense for humans to
read", then why bother implementing "this function that converts
a number to a string and it can return it in any base you want"?
BTW, this is how Ruby and Crystal languages handle conversion
between strings and integers:
puts -10.to_s(16) # prints -a
puts -10.to_s(2) # prints -1010
puts 0x100.to_s(10) # prints 256
puts "10".to_i(2) # prints 2
puts "-100".to_i(8) # prints -64
Basically, strings have method ".to_i" and integers have method
".to_s". A single optional argument specifies base (between 2 and
36) and defaults to 10.
More information about the Digitalmars-d