0

I'm porting some Objective C (a language I have never used...) over to a C# project and have come into the following line:

altStr = [NSString stringWithFormat:@"<altitude>%.16f</altitude>", alt]; //alt === double

In C# I'm pretty sure this translates to:

altStr = string.Format("<altitude>{0}</altitude>", alt); //alt === double

However, I can't find anywhere what %.16f specifically means. I know that f means it's a double, but what about the .16 part? Something to do with number of decimal places?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Stuart Aitken
  • 949
  • 1
  • 13
  • 30
  • 2
    %.16f means: print as a floating point with a precision of sixteen characters after the decimal point – d4zed Jan 13 '20 at 15:02
  • 1
    [It's in the docs](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html)... – J... Jan 13 '20 at 15:03
  • 1
    Does this answer your question? [String Formatting Tricks/Docs](https://stackoverflow.com/questions/2701538/string-formatting-tricks-docs) – J... Jan 13 '20 at 15:03
  • @J... No it didn't answer my question and nothing in the docs says anything about what what `.16` might have meant, or any number for that matter. – Stuart Aitken Jan 13 '20 at 15:07
  • @d4zed Thanks, that's exactly what I needed. – Stuart Aitken Jan 13 '20 at 15:07
  • 1
    @StuartAitken They both answer your question, you just have to read them. It's all in the linked IEEE printf documentation. – J... Jan 13 '20 at 15:13
  • @J... okay so it was in the IEEE documentation, not the Apple developer docs you provided. Got it. – Stuart Aitken Jan 13 '20 at 15:21

1 Answers1

3

%.16f means printing a floating point number, with a precision of 16 digits, so more specific it will translate in C# to the following:

altStr = string.Format("<altitude>{0}</altitude>", Math.Round(alt, 16));
gi097
  • 7,313
  • 3
  • 27
  • 49
  • 1
    Thanks, that cracked it. (Though it should be noted that `Math.Round(alt, 16)` fails because the limit is 15d.p. for Math.Round. I wrote my own rounding method instead) – Stuart Aitken Jan 13 '20 at 15:27