Display 1 number after comma

Hello,

I am a person without programming skills, but I need for a project to display for the begining one number after the comma. My current program displays only the integer part of the number. Could someone support me with this problem??

temp = ((int)v[1]<<7)+ (v[0]>>1);
t1 = (temp/10)%10 + '0';
t0 = temp%10 + '0';
I2C_Start();
I2C_Trans(write);
WRITE_CODE(1, 0xC2);
WRITE_DAT(t1);
I2C_Stop();
What are the types of temp, v[1], v[0], t1, and t0?
Can you give examples of what values would be in v[1] and v[0]?
In other words: Please give input and expected output.

When you refer to comma, you are referring to the decimal separator?

0x2C is the comma character, not 0xC2.
Also, in C++, char is a type of integer, so you can just write WRITE_CODE(1, ',');

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int v[2] = {123, 210};
    
    int temp = ((int)v[1] << 7) + (v[0] >> 1); // no idea
    // equivalent:
    temp = (int)v[1] * 128 + v[0] / 2;
    std::cout << temp << '\n';
    
    char t1 = (temp/10) % 10 + '0'; // 2nd digit of temp (as an ASCII character)
    char t0 = temp%10 + '0'; // 1st (least-significant) digit of temp
    std::cout << t1 << ',' << t0 << '\n';
}


It's in German, but maybe this document helps
http://www.humerboard.at/doku/sb8/uC7.pdf
Last edited on
In my program the v[], t0, t1 are char type. The main purpose of the code is to read temperature from Ds18B20 and display it.
v[0] and v[1] are vectors which are seleting the resolution of 9 bits for my sensor.

I will try to translate the document which you send. Thanks a lot!
Okay, good luck. If you explain what the actual values of v[0] and v[1] are, and what the expected output for those values is, you might be able to get more help.
Last edited on
temp = ((int)v[1]<<7)+ (v[0]>>1);
t1 = (temp/10)%10 + '0'; //this is the integer part of the number
t0 = temp%10 + '0'; //this is the decimal part of the number
I2C_Start();
I2C_Trans(write);
WRITE_CODE(1, 0xC2);
WRITE_DAT(t1);
WRITE_DAT(t0); //this function should be rewrited to show the decimal part of the number
I2C_Stop();
Last edited on
Thanks for your time and explanation.

Regarding:
"
WRITE_DAT(t0); //this function should be rewrited to show the decimal part of the number"


should I use std::cout <<??

Have a nice day!
std::cout << (int) t0;
or
std::cout << floor(t0); // from <cmath>
Thanks for the info!

I will try it as soon as possible :)
Topic archived. No new replies allowed.