summaryrefslogtreecommitdiff
path: root/tikzit/src/common/util.m
diff options
context:
space:
mode:
authorAlex Merry <dev@randomguy3.me.uk>2013-08-27 15:25:26 +0100
committerAlex Merry <dev@randomguy3.me.uk>2013-08-27 15:25:26 +0100
commitad6c6441f4482e12722ae17283dd7a470e3fdcde (patch)
tree81fcf3677139906df6c9932a143d972f01713906 /tikzit/src/common/util.m
parentea86afc07d645c1ba79800fed7c005d444d60f80 (diff)
Make sure we never output numbers in scientific notation
Converting NSNumber to a string raises the possibility of outputing a number like 2.2e-8, as it essentially uses the %g format specifier. This then cannot be parsed. Since there is no built-in specifier for outputing floats with variable precision (ie: removing any trailing zeros), I cooked up a function to do just that. Currently set the maximum precision at 4dp (our normal grid layout only makes use of 2dp).
Diffstat (limited to 'tikzit/src/common/util.m')
-rw-r--r--tikzit/src/common/util.m23
1 files changed, 23 insertions, 0 deletions
diff --git a/tikzit/src/common/util.m b/tikzit/src/common/util.m
index aa21a67..e9b8899 100644
--- a/tikzit/src/common/util.m
+++ b/tikzit/src/common/util.m
@@ -376,5 +376,28 @@ const char *find_start_of_nth_line (const char * string, int line) {
return lineStart;
}
+NSString *formatFloat(CGFloat f, int maxdps) {
+ NSMutableString *result = [NSMutableString
+ stringWithFormat:@"%.*f", maxdps, f];
+ // delete trailing zeros
+ NSUInteger lastPos = [result length] - 1;
+ NSUInteger firstDigit = ([result characterAtIndex:0] == '-') ? 1 : 0;
+ while (lastPos > firstDigit) {
+ if ([result characterAtIndex:lastPos] == '0') {
+ [result deleteCharactersInRange:NSMakeRange(lastPos, 1)];
+ lastPos -= 1;
+ } else {
+ break;
+ }
+ }
+ if ([result characterAtIndex:lastPos] == '.') {
+ [result deleteCharactersInRange:NSMakeRange(lastPos, 1)];
+ lastPos -= 1;
+ }
+ if ([@"-0" isEqualToString:result])
+ return @"0";
+ else
+ return result;
+}
// vi:ft=objc:noet:ts=4:sts=4:sw=4