Last active
November 9, 2016 13:07
-
-
Save blippy/e2695702c46f41f82af76d0f0a99c42e to your computer and use it in GitHub Desktop.
Formatting strings to left and right
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun format-string-left (str width) | |
| (subseq (format nil "~va" width str) 0 width)) | |
| ;;; (format-string-left "hello" 6) => "hello " | |
| ;;; (format-string-left "hello" 4) => "hell" | |
| (defun format-string-right (str width) | |
| (let* ((str1 (format nil "~v@a~a" width " " str)) | |
| (len (length str1)) | |
| (from (- len width))) | |
| (subseq str1 from ))) | |
| ;;; (format-string-right "hello" 6) => " hello" | |
| ;;; (format-string-right "hello" 4) => "ello" | |
| (defun format-decimal (value width dp) | |
| (let* ((as-int (round (* value (expt 10 dp)))) | |
| (as-pos-int (abs as-int)) | |
| (sign (if (< value 0) "-" "+"))) | |
| (format nil "~a~v,'0d" sign width as-pos-int))) | |
| ;;; (format-decimal -3 4 2) => "-0300" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment