I have an unsigned long which is in Watt. I want to convert it to kWatt. So 123456 needs to be converted to 123,456 and be placed into a char array.
What would be the easiest way?
I have an unsigned long which is in Watt. I want to convert it to kWatt. So 123456 needs to be converted to 123,456 and be placed into a char array.
What would be the easiest way?
dtostrf
…after dividing by 1000 !
Serial.print(Watt/1000.0, 3);
I don't know if you can set Serial.print() to use a comma instead of a decimal point. If you must have a comma:
Serial.print(Watt/1000);
Serial.print(',');
int fraction = Watt % 1000;
if (fraction < 100) Serial.print('0'); // Add leading zero
if (fraction < 10) Serial.print('0'); // Add leading zero
Serial.print(fraction);
donno if its the 'easiest' but this is my take on how I would do this!
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
const unsigned long Watts = 1023456789;
char buffer [20];
unsigned int n;
n = sprintf (buffer, "%.03ld", Watts) + 1;
if (Watts>=1000) {
for (int i = 4; i < n; i = i + 3) {
memmove (&buffer[1 + n - i], &buffer[n - i], strlen(buffer));
if (i == 4) buffer[n - i] = '.';
else buffer[n - i] = ',';
}
}
else {
memmove (&buffer[2], &buffer[0], strlen(buffer));
memcpy(buffer,"0.",2);
}
Serial.print(buffer);
Serial.println("kW");
}
void loop() {
// put your main code here, to run repeatedly:
}
1,023,456.789kW
(btw '.' and NOT ',' is used for decimal point. above code was written accordingly for the conversion)
hope that helps...
Thanks.
If I use the following (modified) code:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay (1000);
const unsigned long Watts = 12345678;
char buffer [30];
unsigned int n;
n = sprintf (buffer, "%d", Watts) + 1;
if (n > 4) {
for (int i = 4; i < n; i = i + 3) {
memmove (&buffer[1 + n - i], &buffer[n - i], strlen(buffer));
if (i == 4) buffer[n - i] = '.';
}
}
Serial.print(buffer);
Serial.println("kW");
}
void loop() {
// put your main code here, to run repeatedly:
}
I get this: 14:39:55.525 -> 123345.678kW
Mention the double 3
If Watts = 123456 The output is 14:43:52.137 -> 123.456kW so the number of digits do matter...
What do I have to do if I have 123 Watt and convert it to 0.123 kW?
If I enter 123 I get the following output: 14:42:42.537 -> 123kW
just to confirm, do you want to include the ',' as well in your printed out value?
if not then you would need to replace the 'for' loop inside the 'if' statement with just this:
memmove (&buffer[n - 3], &buffer[n - 4], strlen(buffer));
buffer[n - 4] = '.';
as you removed the ',' insertion statement from the code, the output you got is totally expected.
have a look at the code in reply #5 again, the code has been updated to cater for that!
hope that helps....
Thanks for your answer. I tried to modify the code so that only a '.' is inserted before the last 3 digits. A ',' is not needed. Just a '.' to convert Watts into kWatt.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1500);
const unsigned long Watts = 12345678;
char buffer [20];
unsigned int n;
n = sprintf (buffer, "%ld", Watts) + 1;
if (n > 4) {
for (int i = 4; i < n; i = i + 3) {
memmove (&buffer[n - 3], &buffer[n - 4], strlen(buffer));
buffer[n - 4] = '.';
//else buffer[n - i] = ',';
}
}
else {
n = 5 - strlen(buffer);
memmove (&buffer[n], &buffer[0], strlen(buffer));
memset(buffer, '0', n);
buffer[1] = '.';
}
Serial.print(buffer);
Serial.println("kW");
}
void loop() {
// put your main code here, to run repeatedly:
}
The output is 12345..678kW
when Watts is 123 the output is 0.123
when Watts is 1234 the output is 1.234
So when Watts is more than 6 digits it will get two '..'
I am trying to understand where it goes wrong but probably where you would try to insert the ','
I'm a little surprised not to see the % operator anywhere here.
Hint:
%ld.%03ld\n", Watts / 1000, Watts % 1000
that is exactly why its going wrong.
As mentioned in reply #7, the code I posted was intended to insert '.' and ',' into the printed out value.
if you do not want the ',', then you need to remove the 'for' loop (as already explained).
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1500);
const unsigned long Watts = 12345678;
char buffer [20];
unsigned int n;
n = sprintf (buffer, "%ld", Watts) + 1;
if (n > 4) {
memmove (&buffer[n - 3], &buffer[n - 4], strlen(buffer));
buffer[n - 4] = '.';
}
else {
n = 5 - strlen(buffer);
memmove (&buffer[n], &buffer[0], strlen(buffer));
memset(buffer, '0', n);
buffer[1] = '.';
}
Serial.print(buffer);
Serial.println("kW");
}
void loop() {
// put your main code here, to run repeatedly:
}
but tbh is you just wanted to have the decimal point inserted, there are 'simpler' code as pointed out by others on this thread.
See reply #4.
The OP wanted a decimal comma, so:
Serial.printf("%ld,%03ld kW", Watts / 1000, Watts % 1000);
on third-party models like ESP32 or ESP8266 which support Serial.printf(). On basic models like UNO, Nano, Mega:
char buff[20];
sprintf(buff, "%ld,%03ld kW", Watts / 1000, Watts % 1000);
Serial.print(buff);
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.