- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello!
I'm trying to convert a number (in string format) to the format "abc/def.x"
It's hard to explain the format with words and probably easier to understand with examples, so here are a few examples:
1532.645 should become 1/532.6
3054 should become 3/054.0
5 should be 0/005.0
127.99 should become 0/128.0
So basically, the digits before the / sign should be the original number divided by 1000, rounded down. If the number is less than 1000, there should be a 0 before the / sign.
The orignal number should then be rounded up to 1 decimal point, even when the number is even it must have exactly one decimal.
There must always be three digits (and one decimal point) after the / sign. Add zeros if needed. For example: 1 should become 0/001.0 and the number 45.69 should become 0/045.7
I have a rough idea of dividing the number into substrings and combining them with strcat. To get the digits before the / sign simply divide the number by 1000 and make it integer (by using fix). Add the string "/". But I don't know how to get the last 3 digits (and the decimal point) if the original number has 4 digits or more. Anyone have in idea how to solve this?
Code so far:
(setq original_number 123456.789)
(setq text_1000 (/ original_number 1000))
(setq text_1000 (fix text_1000))
(setq text_100 ??????????????)
(setq final_text (strcat (itoa text_1000) "/" (itoa text_100))
Solved! Go to Solution.