473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Displaying numbers in aligned columns

Don
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b"
is a space character). In other words, I always want the second digit of each number to be in the
same column, so that the beginning of "restOfText" will start in the same column for each record
produced by the following code.

I've tried several things, but can't seem to come up with the answer. Any help would be
appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' + numberOne + '<font
color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #1
12 1314
Don
On Tue, 08 Mar 2005 11:55:54 -0700, Don <no@adr.com> wrote:
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b"
is a space character). In other words, I always want the second digit of each number to be in the
same column, so that the beginning of "restOfText" will start in the same column for each record
produced by the following code.

I've tried several things, but can't seem to come up with the answer. Any help would be
appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' + numberOne + '<font
color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

By the way, "restOfText" is setup to display in "Verdana" font (proportional font). I'm displaying
"numberOne" and "numberTwo" in "Arial" (mono-spaced font).

Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #2
Don wrote:
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b" is a space character). In other words, I always want the second digit of each number to be in the same column, so that the beginning of "restOfText" will start in the same column for each record produced by the following code.

I've tried several things, but can't seem to come up with the answer. Any help would be appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' + numberOne + '<font color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don


The <font> tag was deprecated ages ago. This is really a layout
(HTML/CSS) question, impossible to answer as you've given no details
relating to how the page is set up. 'column' suggests a table, and you
can use CSS 'text-align' to right-justify data.

Jul 23 '05 #3
Don wrote on 08 mrt 2005 in comp.lang.javascript:
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;


function TwoPlacesAtLeast(x){
return ((x+'').length==1)?'&nbsp;'+x:''+x
}

sp1 = '<span style="font-family:courier;">'
sp2 = '</span><br>'

document.write(sp1+TwoPlacesAtLeast(1)+sp2);
document.write(sp1+TwoPlacesAtLeast(12)+sp2);
document.write(sp1+TwoPlacesAtLeast(3)+sp2);
document.write(sp1+TwoPlacesAtLeast(99)+sp2);

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
"Don" <no@adr.com> wrote in message
news:v4********************************@4ax.com...
With the following, I'm trying to display numberOne and numberTwo,
each as a two digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn"
will be displayed (where "b"
is a space character). In other words, I always want the second digit
of each number to be in the
same column, so that the beginning of "restOfText" will start in the
same column for each record
produced by the following code.

I've tried several things, but can't seem to come up with the answer.
Any help would be
appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' +
numberOne + '<font
color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don


If you're using a variable width font then a space isn't necessarily the
same width as a numerical digit character. Also, HTML turns multiple
whitespace into a single space and removes leaving whitespace. If you
want stuff to line up, use <pre></pre> and a fixed width font:

<script type="text/javascript">
function leadingPad(n, d)
{
if (n < (Math.pow(10, d - 1)))
{
return (new Array(d - String(n).length + 1)).join(' ') + n;
}
return n;
}

var numberOne = 1;
var numberTwo = 20;

var htmlString =
'<pre style="font-family:monospace;">' +
leadingPad(numberOne, 2) + '\n' +
leadingPad(numberTwo, 2) +
'</pre>';

document.write(htmlString);
</script>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5
Don
On Tue, 08 Mar 2005 19:43:01 GMT, "Grant Wagner" <gw*****@agricoreunited.com> wrote:
"Don" <no@adr.com> wrote in message
news:v4********************************@4ax.com.. .
With the following, I'm trying to display numberOne and numberTwo,
each as a two digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn"
will be displayed (where "b"
is a space character). In other words, I always want the second digit
of each number to be in the
same column, so that the beginning of "restOfText" will start in the
same column for each record
produced by the following code.

I've tried several things, but can't seem to come up with the answer.
Any help would be
appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' +
numberOne + '<font
color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don


If you're using a variable width font then a space isn't necessarily the
same width as a numerical digit character. Also, HTML turns multiple
whitespace into a single space and removes leaving whitespace. If you
want stuff to line up, use <pre></pre> and a fixed width font:

<script type="text/javascript">
function leadingPad(n, d)
{
if (n < (Math.pow(10, d - 1)))
{
return (new Array(d - String(n).length + 1)).join(' ') + n;
}
return n;
}

var numberOne = 1;
var numberTwo = 20;

var htmlString =
'<pre style="font-family:monospace;">' +
leadingPad(numberOne, 2) + '\n' +
leadingPad(numberTwo, 2) +
'</pre>';

document.write(htmlString);
</script>

Ok, given the following, how do I display "numberOne", "numberTwo", and "restOfText" each in
different colors and the entire thing on just one line? In other words, how do I change the
"inline" style to reflect those changes? Guess I've got a lot to learn about styles.

function leadingPad(n, d)
{
if (n < (Math.pow(10, d - 1)))
{
return (new Array(d - String(n).length + 1)).join(' ') + n;
}
return n;
}

var numberOne = 1;
var numberTwo = 10;
var restOfText = "Hello there!";

htmlString = '<pre style="font-family:monospace; color:blue; font-size:x-small">' +
leadingPad(numberOne, 2) + leadingPad(numberTwo, 2) + restOfText + '</pre>';
Thanks everyone for your help.
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #6
Don wrote:
[...]
Ok, given the following, how do I display "numberOne", "numberTwo", and "restOfText" each in
different colors and the entire thing on just one line? In other words, how do I change the
"inline" style to reflect those changes? Guess I've got a lot to learn about styles.


Learn about the <span> tag and CSS. A useful introduction is
here:

<URL:http://www.w3schools.com/css/default.asp>

Briefly, use a style sheet to define classes with the
characteristics that you want, then apply the classes using
<span> tags around text as required.

[...]
--
Rob
Jul 23 '05 #7
Don wrote:
[...]
<script type="text/javascript">
function leadingPad(n, d)
{
if (n < (Math.pow(10, d - 1)))
{
return (new Array(d - String(n).length + 1)).join(' ') + n;
}
return n;
}

If the intention is simply to put one space in front of single
digit numbers, then try:

function leadingPad(n) {
return ( n < 10 )? '&nbsp;' + n : n;
}

Or for numbers up to and including 999 that need one or two
spaces:

function leadingPad(n) {
return (n<10)? '&nbsp;&nbsp;'+n : (n<100)? '&nbsp;'+n : n;
}

Grant's routine will put a specified number of ' ' characters in
front of the digit, but they will likely be stripped out by the
HTML parser, so maybe use:

return (new Array(d - String(n).length+1)).join('&nbsp;')+n;

[...] Ok, given the following, how do I display "numberOne", "numberTwo", and "restOfText" each in
different colors and the entire thing on just one line? In other words, how do I change the
"inline" style to reflect those changes? Guess I've got a lot to learn about styles.
[...]

Define some classes:

<style type="text/css">
.style1, .style2
{
font-family: monospace;
color: blue;
font-size: x-small;
}
.style2
{
color: red;
}
</style>
htmlString = '<pre style="font-family:monospace; color:blue; font-size:x-small">' +
leadingPad(numberOne, 2) + leadingPad(numberTwo, 2) + restOfText + '</pre>';


Then apply them to your HTML:

htmlString = '<span class="style1">'
+ leadingPad(numberOne, 2)
+ '</span>'
+ '<span class="style2">'
+ leadingPad(numberTwo, 2)
+ '</span>'
+ restOfText;

For the sake of speed, you may consider using an array (this is
much faster where you have a lot of HTML to concatenate into
your string):

htmlString = [
'<span class="style1">',
leadingPad(numberOne, 2),
'</span>',
'<span class="style2">',
'leadingPad(numberTwo, 2),
'</span>',
restOfText // no comma after last element
];
....

document.write(htmlString.join(''));

or maybe:

elementReference.innerHTML = htmlString.join('');

There is a useful introduction to HTML and CSS here:

<URL:http://www.w3schools.com>

You seem to have used '\n' for line breaks, which will work in
some instances (e.g. textarea, alert) but you need '<br>' if you
want a line break in HTML.
--
Rob
Jul 23 '05 #8
RobG wrote:
[...]
For the sake of speed, you may consider using an array (this is
much faster where you have a lot of HTML to concatenate into
your string):

htmlString = [
'<span class="style1">',
leadingPad(numberOne, 2),
'</span>',
'<span class="style2">',
'leadingPad(numberTwo, 2),
Oops, a bit keen with quotes:

leadingPad(numberTwo, 2),
'</span>',
restOfText // no comma after last element
];

[...]
--
Rob
Jul 23 '05 #9
Don
On Tue, 08 Mar 2005 23:45:24 GMT, RobG <rg***@iinet.net.auau> wrote:
RobG wrote:
[...]
For the sake of speed, you may consider using an array (this is
much faster where you have a lot of HTML to concatenate into
your string):

htmlString = [
'<span class="style1">',
leadingPad(numberOne, 2),
'</span>',
'<span class="style2">',
'leadingPad(numberTwo, 2),


Oops, a bit keen with quotes:

leadingPad(numberTwo, 2),
'</span>',
restOfText // no comma after last element
];

[...]

Lots of good stuff for me to learn here! I think I've got the idea now. Thank you very much Rob
for your help. I appreciate it very much.

Regards,
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #10
JRS: In article <v4********************************@4ax.com>, dated
Tue, 8 Mar 2005 11:55:54, seen in news:comp.lang.javascript, Don
<no@adr.com> posted :
With the following, I'm trying to display numberOne and numberTwo, each as a two
digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be
displayed (where "b"
is a space character).


function Pad(S, K) { S += '' // Inputs expected to be numeric.
K -= S.length ; while ( K-->0 ) S = '#' + S ; return S }

var htmlString =
'<font face="Arial" size="2" color="#0000FF">' + Pad(numberOne, 2) +
'<font color="#FF0000">' + Pad(numberTwo, 2) + '</font></font>' +
restOfText;
Since you want to pad at least two variables, a function is appropriate.
If it is absolutely certain that (for K=2) Pad will never ever be called
on a number outside 0..99, the >0 can be omitted. Parameter K can be
omitted and var K set by K = 2 - S.length ; for fixed width.

As written, Pad pads with a # character, for test. With a fixed pitch
font in <pre>, a space can be used; otherwise, you can use &nbsp; and
hope that it gives about the right width. In a rational world, all
characters which can be a part of a number, including separators, would
have the same width as each of the digits - that means that 0 1 2 3 4 5
6 7 8 9 . , + - a b c d e f A B C D E F x X space would all have the
same width as each other.

For perfect alignment, however, you could instead write into a right-
justified field.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11
On Wed, 09 Mar 2005 14:54:21 +0000, Dr John Stockton wrote:
JRS: In article <v4********************************@4ax.com>, dated
Tue, 8 Mar 2005 11:55:54, seen in news:comp.lang.javascript, Don
<no@adr.com> posted :
With the following, I'm trying to display numberOne and numberTwo, each as a two
digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be
displayed (where "b"
is a space character).
<snip> As written, Pad pads with a # character, for test. With a fixed pitch font in <pre>, a space can be used; otherwise, you can use &nbsp; and
hope that it gives about the right width. In a rational world, all
characters which can be a part of a number, including separators, would
have the same width as each of the digits - that means that 0 1 2 3 4 5
6 7 8 9 . , + - a b c d e f A B C D E F x X space would all have the
same width as each other.

For perfect alignment, however, you could instead write into a right-
justified field.


....or put each digit of each number into a fixed-width cell of a
fixed-with table.

--
Life is short, but wide. -KV

Jul 23 '05 #12
Don
On Tue, 08 Mar 2005 11:55:54 -0700, Don <no@adr.com> wrote:
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character
group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b"
is a space character). In other words, I always want the second digit of each number to be in the
same column, so that the beginning of "restOfText" will start in the same column for each record
produced by the following code.

I've tried several things, but can't seem to come up with the answer. Any help would be
appreciated.
var numberOne = numberOne.toString();
if(numberOne.length == 1)
numberOne = " "+numberOne;

var numberTwo = numberTwo.toString();
if(numberTwo.length == 1)
numberTwo = " "+numberTwo;
var htmlString = '<font face="Arial" size="2" color="#0000FF">' + numberOne + '<font
color="#FF0000">' + numbertwo + '</font></font>' + restOfText;
Thanks,
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Thanks everyone for your excellent help!

Regards,
Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rookie | last post by:
I need to display several columns of numbers in a textbox. I wish to display the columns with the decimal point position aligned vertically. I have found that the # digit placeholders do not...
4
by: Enno Middelberg | last post by:
Hi, I'm sure someone asked this question before, but I can't find the solution on the web or in the groups. I want to print out columns of numbers which are aligned at the decimal point,...
25
by: Michael Schuerig | last post by:
I'm trying to do something seemingly very simple, but it's brought me close to crushing my head on the keyboard. All I want is a table where the head row is fixed and the body columns below are...
1
by: Martin | last post by:
Dear group, I have a list in an access97 database that causes me trouble. The problem is that as default all the columns in the (multiselect) list are aligned to the left. I guess the trick...
1
by: genojoe | last post by:
I want to right-align numbers in a combobox. Since there is no TextAlign property for the ComboBox, I tried the following code in a form containing a default combobox. 'Declarations and two...
2
by: tgscott | last post by:
Hi, By default numbers in ordered lists are right aligned, like this... 1. Item 2. Item .... 10. Item 11. Item
0
by: Dave Rado | last post by:
Is it possible with css to make the numbers in a numbered list left- aligned (as in this mock-up where I've used 2-column tables to achieve it: http://tinyurl.com/yv6njd - as opposed to the default...
6
by: yoavyoavyoav | last post by:
Hi There , I have an application that creates a csv file and I want it to display it in excel after creation. I've used to following code - which works well : using ExcelInterop =...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.