❔ How to format float to currency

I am multiplying small numbers (0.08 - 0.18 etc) and getting numbers like 34.44241. I want to figure out how I can multiply these values in terms of USD to be able to come out with a dollar and cent amount.
24 Replies
Anton
Anton2y ago
show the code
dankmememachine
dankmememachineOP2y ago
sure, one moment
Hugh
Hugh2y ago
When working with currency, it’s always more sensible to use int, and have it refer to cents, or to 0.1 cent
dankmememachine
dankmememachineOP2y ago
I am using Unity with monobehaviour, by the way
Anton
Anton2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
dankmememachine
dankmememachineOP2y ago
BlazeBin - xubyufcuxsrt
A tool for sharing your source code with the world!
Timtier
Timtier2y ago
Not decimal due to potential numeric sizes and rounding? 🤔
dankmememachine
dankmememachineOP2y ago
I am trying to keep this like an inventory of the amount of food i have, i want to know the numerical value of food and that would be in USD $ the main issue is that some costs come out to fractions of cents it seems, with each ingredient right now costing 0.234 or 0.468 (and this cannot change)
Anton
Anton2y ago
not decimal because unity can't serialize that presumably
dankmememachine
dankmememachineOP2y ago
Decimal as a type can be used I belive i can still use System; etc and other 'normal' C# practices
Anton
Anton2y ago
not gonna be visible in the editor
dankmememachine
dankmememachineOP2y ago
hmm ok, any other way i can be able to convert this stuff to dollars and cents?
Anton
Anton2y ago
float is fine too move the 5 into a constant at least, or a serialized field so what doesn't work?
dankmememachine
dankmememachineOP2y ago
well in the current state i get numbers like 34.356 etc
Timtier
Timtier2y ago
Fair, intresting 👀
Anton
Anton2y ago
and you only want 34 or what?
dankmememachine
dankmememachineOP2y ago
I want a conversion as closely as possible to dollars and cents
Anton
Anton2y ago
so you want 34 dollars and 36 cents?
dankmememachine
dankmememachineOP2y ago
I think I was able to complete it doing this:
Debug.Log("Total charge for Tacos: " + string.Format("{0:C}", totalChargeForTacos));
Debug.Log("Total charge for Tacos: " + string.Format("{0:C}", totalChargeForTacos));
DaVinki
DaVinki2y ago
If you're doing this then you might actually want to use long. $21,474,836.47 would be the most amount of money something would be able to have if you used Int32 but with Int64 that max amount becomes $92,233,720,368,547,758.07
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server