Center numbers in a cell

I decided I wanted to write a simple spreadsheet program. Just adding, subtracting, etc., in specified cells. Each cell is 13 characters wide. I would like to be able to center a number or decimal entered, or the result of the math done on the the desired cells.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
void Print_Spreadsheet (vector<vector<Cell>>& Spreadsheet, int column, int row)
{
	int a, b, wide = 11, high=18, len, num;
	double fl;
	string rub(13,' ');//Remove contents of entered cell
	for( a=1;a<wide;a++)
	{
		gotoXY(7+(14*a), 1, Spreadsheet[0][a+column].text + " ");
		for( b=1;b<high;b++)
		{
			gotoXY(6, 1+(b*2));
			if(Spreadsheet[b+row][0].number < 10)
				cout << " ";
			cout << Spreadsheet[b+row][0].number;
			gotoXY(1+(14*a), 1+(b*2),rub);

			if(Spreadsheet[b+row][a+column].number != -1)
			{
				len=6;// Start. Centers the results
				num = Spreadsheet[b+row][a+column].number;
				if(num>99)
					len-=1;
				if(num>999)
					len-=1;
				if(num>9999)
					len-=1;
				if(num>99999)
					len-=1;// Keeps subtracting the larger
                                               // number gets. Works ok for decimals
				gotoXY(1+len+(14*a), 1+(b*2), num);
			}
			else if(Spreadsheet[b+row][a+column].dubl > -1)
			{
				len=6;
				fl = Spreadsheet[b+row][a+column].dubl;
				if(fl > 99.001)
					len-=1;
				if(fl > 999.001)
					len-=1;
				if(fl > 9999.001)
					len-=1;
				if(fl > 99999.001)
					len-=1;
				gotoXY(1+len+(14*a), 1+(b*2));
				cout << fl;
                             // Doesn't work well with doubles
			}
			else if(Spreadsheet[b+row][a+column].text != ".")
			{
				Spreadsheet[b+row][a+column].text.erase( 0, Spreadsheet[b+row][a+column].text.find_first_not_of(' ') );
				len = (Spreadsheet[b+row][a+column].text.length())/2;
				gotoXY((7-len)+(14*a), 1+(b*2), Spreadsheet[b+row][a+column].text);
			}
			if(Spreadsheet[b+row][a+column].function > "")
			{
				Do_Math(Spreadsheet, a+column, b+row);
                               // Above does the math only. No printing
			}
		}
	}
}


How can I center decimal numbers? Or even integers, more efficiently? I have 10 columns across the screen, and 17 rows of cells.

Thanks in advance for anyone's insight and help.
( If anyone would like to see the whole program, I can put it in dropbox, and let you D/L the source. It's 22,000+ bytes )
Last edited on
At the point of writing the value to the cell, turn the value into a string and then add some spaces to the front of the string.

How many spaces?

(13 - string_length) / 2

That's how many
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <sstream>

namespace detail
{
    template < typename T > struct centre
    {
        explicit centre( std::size_t cell_width, const T& value )
            : cell_width(cell_width), value(value) {}
        std::size_t cell_width ;
        const T& value ;
    };

    template < typename T >
    std::ostream& operator<< ( std::ostream& stm, const centre<T>& cntr )
    {
        std::ostringstream str_stm ;
        str_stm.imbue( stm.getloc() ) ;
        str_stm.flags( stm.flags() ) ;
        str_stm.precision( stm.precision() ) ;

        str_stm << cntr.value ;
        const std::string str = str_stm.str() ;
        if( str.size() < cntr.cell_width )
        {
            const auto diff = cntr.cell_width - str.size() ;
            return stm << std::string( diff/2, ' ' ) << str
                       << std::string( (diff+1)/2, ' ' ) ;
        }
        else return stm << str ;
    }
}

template < typename T > detail::centre<T> centre( std::size_t cell_width, const T& value )
{ return detail::centre<T>(cell_width,value) ; }

int main()
{
    std::cout << "|012345678901234567890123456789|\n"
              << '|' << centre( 10, 123 ) << "|\n"
              << '|' << std::showbase << std::hex << centre( 16, 0x123 ) << "|\n"
              << '|' << centre( 25, "hello world!" ) << "|\n"
              << '|' << std::fixed << std::setprecision(2) << std::showpos
              << centre( 12, 12.345 ) << "|\n" ;
}

http://coliru.stacked-crooked.com/a/374470721f73418a
@Repeater

I tried what you suggested, but more numbers were being added into the float number being converted. I would enter something like, 123.50, and in would be converted to something like, 123.499009. The number I typed would appear in the spreadsheet cell, but the sting length would come out as the converted second number. I had forgo that idea. Thank you though for your help.

@JLBorges

I copied your namespace and template into my Spreadsheet program. I just had to use the last line in your code, and the float was centered in the cell. Thanks. I'm much happier with my programming endeavor now.
123.50, and in would be converted to something like, 123.499009.

Actually, I suspect if you entered 123.50 that would not be converted, because a floating point number can store that value.

Nonetheless, time for you to learn about floating point numbers.

Here's the key thing for you to remember; a floating point number CANNOT store every decimal. In fact, it can store almost none of them.

If you try to store the value 123.40 in a floating point number, it WILL NOT store the value 123.40

If you try to store the value 123.41 in a floating point number, it WILL NOT store the value 123.41

You can experiment with it yourself to see what numbers can actually be stored; https://www.h-schmidt.net/FloatConverter/

https://floating-point-gui.de/
Last edited on
Topic archived. No new replies allowed.