|
Thanks for the reply Stacy. These are all great points. For this project, I'm in JavaScript/TypeScript and dealing with money. So there is no decimal type. But, after this chat I decided to just add two extra decimal places of resolution. So, I'll store a currency amount as 1.1234 and only round it off to 2 during reporting.
Stacy Dudovitz wrote: Conversions: Be very very careful about mixing types i.e. decimal, float and double. Tru dat. Not sure about C#, but in JavaScript/TypeScript I only have one level of precision from a data type. As a bonus though, there is a cool way to help to avoid mixing faux types.
export type Distinct<T, DistinctName> = T & {
__TYPE__: DistinctName
};
export type NumericTypeOne = Distinct<number, 'NumericTypeOne'>;
export type NumericTypeTwo = Distinct<number, 'NumericTypeTwo'>;
Stacy Dudovitz wrote: Implicit Operators in C#: How To Simplify Type Conversions If I'm ever in C/C++ land again I'll check it out. Thanks.
Jeremy Falcon
|
|
|
|
|
I was a bit alarmed by your reply and solution below:
For this project, I'm in JavaScript/TypeScript and dealing with money. So there is no decimal type. But, after this chat I decided to just add two extra decimal places of resolution. So, I'll store a currency amount as 1.1234 and only round it off to 2 during reporting.
There are two possible solutions:
1) If you are always/only going to traffic in money, a more robust solution would be to use integer math and display formatting.
As an example, the value '$1.23" would be stored in an integer of sufficient size to house the min/max dollar value you wish to traffic in. Using RegEx, it would be trivial to strip off the '$' and '.', yielding a value of the price offset by a factor of 100. To display, you could use simple formatting. You can store the values as-is to a data store, or, if you require marshaling of values, divide the value by 100 by first casting the value to float and dividing by 100f.
In this case, I would use Number or BigInt. A quick search on the largest integer type gives the following results:
The biggest integer type in JavaScript is BigInt. It was introduced in ECMAScript 2020. BigInts can store integers of any size, while the Number type can only store integers between -(2^53 - 1) and 2^53 - 1.
2) You could incorporate decimal.js into your project, which will provide you with the decimal type you seek.
You can find that here:
https://mikemcl.github.io/decimal.js/# :text=decimal.-,js,available%20in%20the%20console%20now.
Whichever way you choose, I would implore you NOT to add arbitrary/additional numbers to the right of the decimal place. It will come back to bite you!
|
|
|
|
|
I started with integer math. Moved away from it after finding out that doing to 4 places makes it match up well enough. About the library, decimal.js, it's too bloated for my needs (fast calculations) and doesn't really offer anything that I didn't have when I just was rounding everything.
Jeremy Falcon
|
|
|
|
|
Maybe you want a currency library? You can control the rounding too with this.
currency.js[^]
|
|
|
|
|
Wordle 1,006 5/6*
🟨⬛⬛⬛🟩
🟩🟩🟩⬛🟩
🟩🟩🟩⬛🟩
🟩🟩🟩⬛🟩
🟩🟩🟩🟩🟩
|
|
|
|
|
Wordle 1,006 4/6
🟨🟨⬜🟨⬜
🟨⬜🟨🟨⬜
⬜⬜🟩🟩🟩
🟩🟩🟩🟩🟩
|
|
|
|
|
Wordle 1,006 2/6
🟩⬛⬛⬛⬛
🟩🟩🟩🟩🟩
|
|
|
|
|
Wordle 1,006 4/6
⬜🟨⬜🟨🟩
🟩⬜🟩⬜🟩
🟩⬜🟩🟩🟩
🟩🟩🟩🟩🟩
|
|
|
|
|
Wordle 1,006 3/6
🟨🟨🟩⬛⬛
🟩🟩🟩⬛🟩
🟩🟩🟩🟩🟩
Jeremy Falcon
|
|
|
|
|
Wordle 1,006 5/6
🟩⬜🟩⬜🟩
🟩⬜🟩⬜🟩
🟩⬜🟩🟩🟩
🟩🟩🟩⬜🟩
🟩🟩🟩🟩🟩
Fat fingered the penultimate attempt.
|
|
|
|
|
Wordle 1,006 4/6*
🟨⬜⬜🟨🟩
🟩🟩🟩⬜🟩
🟩🟩🟩⬜🟩
🟩🟩🟩🟩🟩
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
⬜⬜🟩⬜🟩
⬜⬜⬜⬜🟨
⬜⬜🟩🟨🟩
🟩🟩🟩🟩🟩
In a closed society where everybody's guilty, the only crime is getting caught. In a world of thieves, the only final sin is stupidity. - Hunter S Thompson - RIP
|
|
|
|
|
Wordle 1,006 5/6
⬜⬜⬜⬜🟨
⬜🟩🟨⬜⬜
⬜🟩🟩🟨🟩
🟩🟩🟩⬜🟩
🟩🟩🟩🟩🟩
|
|
|
|
|
Wordle 1,006 4/6*
⬜🟨🟩⬜🟨
🟩⬜🟩⬜🟩
🟩⬜🟩🟩🟩
🟩🟩🟩🟩🟩
Happiness will never come to those who fail to appreciate what they already have. -Anon
And those who were seen dancing were thought to be insane by those who could not hear the music. -Frederick Nietzsche
|
|
|
|
|
Wordle 1,006 3/6
🟨🟨⬛⬛🟩
🟩⬛🟩🟩🟩
🟩🟩🟩🟩🟩
Ok, I have had my coffee, so you can all come out now!
|
|
|
|
|
That tasted funny.
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
Give a shortbread girl scout cookie to chickens.
They will demolish them. Ours get so happy when we bring cookies.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
We ran an Outfitters for a while and sold minnow and worm bait. When minnows died we would through them out and our chickens went nuts. They knew first thing in the morning was the time we threw them out and they would be waiting.
Cuckoo for CocoPuffs.
Definition of a burocrate; Delegate, Take Credit, shift blame.
PartsBin an Electronics Part Organizer - Release Version 1.3.1 JaxCoder.com
Latest Article: EventAggregator
|
|
|
|
|
'Cuckoo for fatheads' doesn't have the same ring to it.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
We were visiting a farm in South America when a large tarantula came walking across the yard- big mistake
Aflockof chickens came out of their pen and in 15 seconds pecked the poor spider to death and ate it. Not even a hair was left.
It was unfiltered joy and expert workmanship.
CQ de W5ALT
Walt Fair, Jr.PhD P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
The tarantula won't be trying that again!
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: Give a shortbread girl scout cookie to chickens.
Somehow "cookie" was missing when I first read that and parsed it in my head.
I was wondering, chickens can go cannibal???
|
|
|
|
|
A chicken eating people is not cannibalism. A chicken eating chicken is cannibalism. And yes, chickens can and do eat chicken.
Within you lies the power for good - Use it!
|
|
|
|
|
You're right.
But I still don't dare raise the question, what do you call a chicken that eats people. Other than "nightmarish".
[Edit]
And I know that pigs love bacon...
modified 21-Mar-24 13:37pm.
|
|
|
|
|
Try giving a cat prawns.
Given that they hate getting wet, how the heck did they develop a taste for seafood?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|