Maniacbug's Ethercard/Pachube sketch

I'm using Maniacbug's ethercard sketch Example to use EtherCard on Nanode to upload data to Pachube · GitHub to send data from my Nanode to Pachube, and wondered if it would be possible to send data to 3 decimal places, such as 3.123 instead of whole numbers?
My data variables are sd1, sd2 etc, and the section which formats the data is;

printf_P(PSTR("Sending...\n\r"));

    byte sd = stash.create();
    stash.print("sd1,");
    stash.println((word) sd1 / 1000);  // divide by 1000 to convert watts to kiloWatts
    stash.print("sd2,");
    stash.println((word) sd2 / 1000);  //divide by 1000 to convert watts to kiloWatts
    stash.print("sd3,");
    stash.println((word) sd3);
    stash.save();
    
    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "X-PachubeApiKey: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();

Currently if the sd1 data is 3124, it is uploaded to Pachube as 3 (after being divided by 1000).

pauldreed:
I'm using Maniacbug's ethercard sketch Example to use EtherCard on Nanode to upload data to Pachube · GitHub to send data from my Nanode to Pachube, and wondered if it would be possible to send data to 3 decimal places, such as 3.123 instead of whole numbers?
My data variables are sd1, sd2 etc, and the section which formats the data is;

printf_P(PSTR("Sending...\n\r"));

byte sd = stash.create();
   stash.print("sd1,");
   stash.println((word) sd1 / 1000);  // divide by 1000 to convert watts to kiloWatts
   stash.print("sd2,");
   stash.println((word) sd2 / 1000);  //divide by 1000 to convert watts to kiloWatts
   stash.print("sd3,");
   stash.println((word) sd3);
   stash.save();
   
   // generate the header with payload - note that the stash size is used,
   // and that a "stash descriptor" is passed in as argument using "$H"
   Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                       "Host: $F" "\r\n"
                       "X-PachubeApiKey: $F" "\r\n"
                       "Content-Length: $D" "\r\n"
                       "\r\n"
                       "$H"),
           website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

// send the packet - this also releases all stash buffers once done
   ether.tcpSend();




Currently if the sd1 data is 3124, it is uploaded to Pachube as 3 (after being divided by 1000).

If you have an operation between two integers, the result will be an integer. So, 2 / 2 = 1, 5 / 2 = 2 and so on (the processor truncates the rest of the division). If do you want a float result you need to have one of the operands as a flot, so 5 / 2.0 = 2.5, since 2.0 is a float. Just replace the "1000" by "1000.0".

alvarojusten:
.... Just replace the "1000" by "1000.0".

So to be clear, do I have to declare sd1 & sd2 as 'float's (they are currently declared as 'double'), as well as including the .0 after 1000?
Also, what determines/limits the accuracy of the result, ie 10 / 3 = 3.333333 etc. In other words, can the result be limited to 3 decimal places?

So to be clear, do I have to declare sd1 & sd2 as 'float's (they are currently declared as 'double'), as well as including the .0 after 1000?

Floats and doubles are the same size on the Arduino, so it doesn't matter which you use.

Also, what determines/limits the accuracy of the result, ie 10 / 3 = 3.333333 etc. In other words, can the result be limited to 3 decimal places?

The accuracy of the result is based on the accuracy of the data that was fed in, and the division algorithm in the avr code. That has little to do with the number of decimal places that you see in the result. The accuracy is generally good to 7 decimal places max. How you display that number in other places has (almost) nothing to do with the accuracy of the calculation. You are free to display the value with 48 digits after the decimal point, but doing so won't make it more accurate. You are free to display the value with fewer than 7 decimal places.

You should say something about what the stash object it. It may be converting the value to a string to send somewhere. If so, it may be derived from the Print class. If so, you can control the number of decimal places in the string representation of the number.

PaulS:

So to be clear, do I have to declare sd1 & sd2 as 'float's (they are currently declared as 'double'), as well as including the .0 after 1000?

Floats and doubles are the same size on the Arduino, so it doesn't matter which you use.

Also, what determines/limits the accuracy of the result, ie 10 / 3 = 3.333333 etc. In other words, can the result be limited to 3 decimal places?

The accuracy of the result is based on the accuracy of the data that was fed in, and the division algorithm in the avr code. That has little to do with the number of decimal places that you see in the result. The accuracy is generally good to 7 decimal places max. How you display that number in other places has (almost) nothing to do with the accuracy of the calculation. You are free to display the value with 48 digits after the decimal point, but doing so won't make it more accurate. You are free to display the value with fewer than 7 decimal places.

You should say something about what the stash object it. It may be converting the value to a string to send somewhere. If so, it may be derived from the Print class. If so, you can control the number of decimal places in the string representation of the number.

Well after a number of attempts, I have found that the following works well, to 3 decimal places;

    byte sd = stash.create();
    stash.print("sd1,");
    stash.println((word) sd1 / 1000, 3);  // divide by 1000 to convert watts to kiloWatts, , 3 to 3 decimal places.
    stash.print("sd2,");
    stash.println((word) sd2 / 1000, 3);  //divide by 1000 to convert watts to kiloWatts,  , 3 to 3 decimal places.
    stash.print("sd3,");
    stash.println((word) sd3);
    stash.save();