0

Hi I have a program that does not work right as I aspect:

v = tempC;
h = tempC * 10;
h = h % 10;
memset(stempCA, 0, sizeof(stempCA));
sprintf(stempCA, "%+.2d.%d", v, h);

it works fine on positive temperature "+11.22" but not for negative values : "-02.-1"

where is my mistake? thanx

Gerben
11.3k3 gold badges22 silver badges34 bronze badges
asked Jan 22, 2017 at 15:06
1
  • > where is my mistake? test the temperature first to see if it is negative or positive. your code works for positive temperatures only. Commented Jan 22, 2017 at 16:23

1 Answer 1

1

I think, that -1%10=-1, not 9 as you would like. I use

if (tempC<0) {print'-';tempC=-tempC;};
print tempC as usual for positive numbers
answered Jan 22, 2017 at 15:54
2
  • no , I put it in a Buffer for LCD display, I don't use print : sprintf(Buffer2, "U:%s A:%s", stempCU, stempCA); Commented Jan 22, 2017 at 16:16
  • Got i myself: sprintf(stempCA, "%+.2d.%d", v, abs(h)); Commented Jan 22, 2017 at 16:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.