0

Simple question, but I can't seem to find an answer. If I am trying to display a battery voltage, for example, a difference of a few decimal points can be very important. If I have this:

stringFromNumber(voltage) + 'V'

and voltage is 12.4, it will output 12V. This is no good. I could quite easily fix this by editing the resultant StyleKit, but I would have to do that every time I made a change, which would be a real pain. Does anyone have a solution?

I am working with the C# output, but the problem exists with both C# and Objective-C.

Relevant Code:

In PaintCode I have a Voltage String expression of stringFromNumber(voltage) + 'V' which generates:

private void DrawGuage(float voltage) {  
    ...  
    var voltageString = new NSString(Math.Round(voltage).ToString()) + "V";
    ...

I want PaintCode to generate the same, but without the Math.round.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
EddBC
  • 71
  • 6
  • 1
    Can you show how you're declaring and obtaining `voltage`? A complete example would be helpful, even with hard-coded values. – Jon Skeet Jun 18 '14 at 13:56
  • Just FYI, it's pretty difficult to customize code generated by PaintCode. I usually use PaintCode's output as a "starting point" and modify it for my needs. Doing things like changing string values and colors by variables is pretty easy if you learn how to use the CoreGraphics APIs and understand what PaintCode is doing. – jonathanpeppers Jun 18 '14 at 18:29

2 Answers2

0

I found a way. It's a bit messy, but it works.

stringFromNumber(floor(voltage)) + (
        frac(voltage) != 0 ? '.'+stringFromNumber(frac(voltage)*10) : ''
) + 'V'

Also, I got in touch with PaintCode support, and they said they would think about adding a better way of doing this in a future version, but had a temporary fix similar to mine :

stringFromNumber(round(number)) + "." + stringFromNumber(round(number * 100 % 100))
Tricertops
  • 8,492
  • 1
  • 39
  • 41
EddBC
  • 71
  • 6
  • Formatting numbers is pretty complex task with many parameters. Number of fractional digits, zero padding of trailing fraction digits, decimal separator (for current language) are first to come to mind. The function would become pretty complicated. What about formatting the string in your code and passing as argument? _I just noticed how old this Q&A is. However, the topic is still relevant today._ – Tricertops Jan 30 '16 at 10:01
0

The mentioned PaintCode answer is not 100%. if you have a value a x.5 it will be rounded to x+1 and then get's the 0.5 attached, so instead of 0.5 you will get 1.5 or instead of 10.7 you will get 11.7. So you have to subtract 1 of your decimal number is 0.5 or higher.

So I have played with some mathematical functions and PaintCode. And I'm using an expression with used to use a boolean if the number is the same or above .5, if so it will subtract 1 number of the dominator. This is the function I used as an expression value in PaintCode.

(round(number * 100 % 100) < 50)?(stringFromNumber(round(number)) + "." + stringFromNumber(round(number * 100 % 100))):(stringFromNumber(round(number)-1) + "." + stringFromNumber(round(number * 100 % 100)))
Tricertops
  • 8,492
  • 1
  • 39
  • 41
martin
  • 11
  • 2
  • Maybe it would be easier to format numbers in code using proper `NSNumberFormatter` with desired settings and current user language and then passing the string as argument. – Tricertops Jan 30 '16 at 10:09