❔ 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
show the code
sure, one moment
When working with currency, it’s always more sensible to use int, and have it refer to cents, or to 0.1 cent
I am using Unity with monobehaviour, by the way
$code
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/BlazeBin - xubyufcuxsrt
A tool for sharing your source code with the world!
Not decimal due to potential numeric sizes and rounding? 🤔
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)
not decimal because unity can't serialize that presumably
Decimal as a type can be used I belive
i can still use System; etc and other 'normal' C# practices
not gonna be visible in the editor
hmm ok, any other way i can be able to convert this stuff to dollars and cents?
float is fine too
move the 5 into a constant at least, or a serialized field
so what doesn't work?
well in the current state i get numbers like 34.356 etc
Fair, intresting 👀
and you only want 34 or what?
I want a conversion as closely as possible to dollars and cents
so you want 34 dollars and 36 cents?
I think I was able to complete it doing this:
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
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.