What would be the most efficient way of converting a long to a decimal format followed by the place name, e.g.
1,498,000,000,000 - 1.498 Trillion
or
2,147,000,000 - 2.147 Billion
The operation has to perform as quick as possible, thanks =)
What would be the most efficient way of converting a long to a decimal format followed by the place name, e.g.
1,498,000,000,000 - 1.498 Trillion
or
2,147,000,000 - 2.147 Billion
The operation has to perform as quick as possible, thanks =)
The most efficient way to do it (from a runtime performance perspective) would be to write a custom formatter from the ground up, and spend a few more days, testing it, profiling it and optimizing it.
However, the most efficient way to do this from a perspective of not wasting valuable Software Developer time would be to just one of the existing formatter classes for the first part, and some simple custom code to do the scaling for the last part. Then you profile the entire application, and decide whether it is REALLY worthwhile to optimize the formatting. (And my bet is that it wouldn't be.)
For the "relative string" part, I'd just implement this as a chain of if
/ else
tests to select a scale factor and unit (i.e. thousand, million, billion, trillion). Then divide the input number by the scale factor, convert to a double and format in a fixed-width field using the standard APIs.
Simple, easy to understand ... and you can now move on to the next part of your application.