Format problems with calories, totalAscent and heading

I got problems formatting these 3 fields i'm doing these strings and return no data

   textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, fields.power != null ? lapPower : calories.format("%.1f") );

only if i do it this way but then i can not use format. 

textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, fields.calories );

}

i also made a format string in a background file and on the compute info and its not working

     function formatCalories(calories) {

       calories = info.calories;

        if (info.calories != null) {

            return "0";}

            else {

        return calories.format("%.2f");

what im doing wrong??

  • if calories are not equal null, you return "0"

    if calories are equal null, you try to do the format() on that.

    change the != to == so if it's ==null, you return "0"

  • thanks but stills not working this is the repository 

    https://github.com/xuxumatu/PowerEdgeField

    im trying to do a field that show calories instead of power if the powermeter is not present, it only works when i use fields.calories, or fields.totalAscent but with unformatted numbers or where i use format.calories("%.2f") it returns no number

  • not working this is the repository 

    https://github.com/xuxumatu/PowerEdgeField

    That link isn't valid, and I'm not seeing any project in your github account at all.

    You should theoretically be able to do this:

        hidden var _currentPower;
        hidden var _calories;
    
        function compute(info) {
            if (info has :currentPower) {
                _currentPower = info.currentPower;
            }
            
            if (info has :calories) {
                _calories = info.calories;
            }
        }
        
        function onUpdate(dc) {
            dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
            dc.clear();
    
            var value = null;
            
            if (_currentPower != null) {
                value = _currentPower.format("%0.2f");
            } else if (_calories != null) {
                value = _calories.format("%0.1f");
            } else {
                value = "---";
            }
    
    		var cx = dc.getWidth() / 2;
    		var cy = dc.getHeight() / 2;
    		var font = Graphics.FONT_SMALL;
    		var justification = Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER;
    
    		dc.drawText(cx, cy, font, value, justification);
        }

    That said, both the currentPower and calories fields are of type Number, so displaying even one digit after the decimal point doesn't really make a lot of sense since those digits should always be 0.

  • Thanks a lot but it didn't work i think it's a bug specifically with calories because i have other strings like this on the same block that work: 

    textC(dc, 25, 9, Gfx.FONT_TINY, (fields.altitude).format("%.0f") );

    it is working by now without the zero on start, this is the app
    https://apps.garmin.com/es-ES/apps/3b33a74f-341a-40ef-854e-69acd180a0b9

    i'll upload a new repository with the changes


    i tried this

    if (fields.power != null) {

    textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, lapPower : “0”);

    } else {

    textC(dc, 50, 159, Gfx.FONT_NUMBER_MEDIUM, fields.calories : "0");

    }

    an this

    if (fields.power == null) {

    textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, calories.format("%.1f"));

    } else {

    textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, lapPower);

    }

    and did not work, this is the one is working without zero

    if (fields.power == null) {

    textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, fields.calories);

    } else {

    textC(dc, 50, 189, Gfx.FONT_NUMBER_MEDIUM, lapPower);

    }