Computer Programming: Rounding, Dates, Line Input Prompts

In summary, to round a number to a certain number of decimal places, you can use the print using command. To use dates (month day, year) as response to input as a single variable, you can use the date function. To use a “line input prompt”, you can use the input command.
  • #1
mewhoexactlywhat
44
0
There are a few things that I can't remember learning in my Computer Programming class, and I haven't been able to find the answers. Can anyone tell me how to:
-round a number to a certain number of decimal places?
-use dates (month day, year) as response to input as a single variable?
-how/why to use a "line input prompt"?
Thank you.
 
Technology news on Phys.org
  • #2
round a number to a certain number of decimal places?

I guess you could use the print using command.

PRINT USING

Suppose you want to add $12.47 to $1.03. The correct answer is $13.50. This almost works:

PRINT 12.47 + 1.03

It makes the computer print:

13.5

But instead of 13.5, we should try to make the computer print 13.50.

This command forces the computer to print 13.50:

PRINT USING "##.##"; 12.47 + 1.03

The “##.##” is called the picture or image or format: it says to print two characters, then a decimal point, then two digits. The computer will print:

13.50

This command puts that answer into a sentence:

PRINT USING "You spent ##.## at our store"; 12.47 + 1.03

The computer will print:

You spent 13.50 at our store

Rounding This program makes the computer divide 300 by 7 but round the answer to two decimal places:

CLS

PRINT USING "##.##"; 300 / 7

When the computer divides 300 by 7, it gets 42.85714, but the format rounds the answer to 42.86. The computer will print:

42.86

Multiple numbers Every format (such as “###.##”) is a string. You can replace the format by a string variable:

CLS

a$ = "###.##"

PRINT USING a$; 247.91

PRINT USING a$; 823

PRINT USING a$; 7

PRINT USING a$; -5

PRINT USING a$; -80.3

The computer will print:

247.91

823.00

7.00

-5.00

-80.30

When the computer prints that column of numbers, notice that the computer prints the decimal points underneath each other so that they line up. So to make decimal points line up, say PRINT USING instead of just PRINT.

To print those numbers across instead of down, say this:

PRINT USING "###.##"; 247.91; 823; 7; -5; -80.3

It makes the computer print 247.91, then 823.00, etc., like this:

247.91823.00 7.00 -5.00-80.30

Since the computer prints those numbers so close together, they’re hard to read. To make the computer insert extra space between the numbers, widen the format by putting a fourth “#” before the decimal point:

PRINT USING "####.##"; 247.91; 823; 7; -5; -80.3

Then the computer will print:

247.91 823.00 7.00 -5.00 -80.30

If you say —

PRINT USING "My ## pals drank ###.# pints of gin"; 24; 983.5

the computer will print:

My 24 pals drank 983.5 pints of gin

Oversized numbers Suppose you say:

PRINT USING "###.##"; 16238.7

The computer tries to print 16238.7 by using the format “###.##”. But since that format allows just three digits before the decimal point, the format isn’t large enough to fit 16238.7. So the computer must disobey the format. But the computer also prints a percent sign, which means, “Warning! I am disobeying you!” Altogether, the computer prints:

%16238.70

Final semicolon At the end of the PRINT USING statement, you can put a semicolon:

CLS

PRINT USING "##.##"; 13.5;

PRINT "credit"

Line 2 makes the computer print 13.50. The semicolon at the end of line 2 makes the computer print “credit” on the same line, like this:

13.50credit

Advanced formats Suppose you’re running a high-risk business. On Monday, your business runs badly: you lose $27,931.60, so your “profit” is minus $27,931.60. On Tuesday, your business does slightly better than break-even: your net profit for the day is $8.95.

Let’s make the computer print the word “profit”, then the amount of your profit (such as -$27,931.60 or $8.95), then the word “ha” (because you’re cynical about how your business is going).

You can do that printing in several ways. Let’s explore them.…

If you say —

CLS

a$ = "profit######.##ha"

PRINT USING a$; -27931.6

PRINT USING a$; 8.95

the computer will print:

profit-27931.60ha

profit 8.95ha

If you change the format to “profit###,###.##ha”, the computer will insert a comma if the number is large:

profit-27,931.60ha

profit 8.95ha

If you change the format to “profit+#####.##ha”, the computer will print a plus sign in front of any positive number:

profit-27931.60ha

profit +8.95ha

To print a negative number, the computer normally prints a minus sign before the number. That’s called a leading minus. You can make the computer put the minus sign after the number instead; that’s called a trailing minus. For example, if you change the format to “profit######.##-ha”, the computer will print a minus sign AFTER a negative number (and no minus after a positive number), like this:

profit27931.60-ha

profit 8.95 ha

Normally, a format begins with ##. If you begin with $$ instead (like this: “profit$$#####.##ha”), the computer will print a dollar sign before the digits:

profit-$27931.60ha

profit $8.95ha

If you begin with ** (like this: “profit**#####.##ha”), the computer will print asterisks before the number:

profit*-27931.60ha

profit******8.95ha

If you begin with **$ (like this: “profit**$#####.##ha”), the computer will print asterisks and a dollar sign:

profit*-$27931.60ha

profit******$8.95ha

When you’re printing a paycheck, use the asterisks to prevent the employee from enlarging his salary. Since the asterisks protect the check from being altered, they’re called check protection.

You can combine several techniques into a single format. For example, you can combine the comma, the trailing minus, and the **$ (like this: “profit**$##,###.##-ha”), so that the computer will print:

profit**$27,931.60-ha

profit*******$8.95 ha

If you change the format to “profit##.#####^^^^ha”, the computer will print numbers by using E notation:

profit-2.79316E+04ha

profit 8.95000E+00ha
 
  • #3
mewhoexactlywhat said:
There are a few things that I can't remember learning in my Computer Programming class, and I haven't been able to find the answers. Can anyone tell me how to:
-round a number to a certain number of decimal places?
-use dates (month day, year) as response to input as a single variable?
-how/why to use a "line input prompt"?
Thank you.

best to forget QBasic and use python. QBasic is a horrid language that promotes bad programing.
 
  • #4
If you like Python then you'll love VPython - a free add-on that let's you do 3D graphics animation very easily!
 
  • #5
My first language was qbasic..
 
  • #6
Bio-Hazard said:
My first language was qbasic..

and it is a crappy language.
 
  • #7
I won't say its a crappy language. Its just not as versatile as other programming languages. Its a easy language to learn; meant for beginners. Visual BASIC was the first programming language I learnt. I moved on to other languages such as C, and we I look back a BASIC I can see how limited it was, but it certainly was not crappy.
 
  • #8
ranger said:
I won't say its a crappy language. Its just not as versatile as other programming languages. Its a easy language to learn; meant for beginners. Visual BASIC was the first programming language I learnt. I moved on to other languages such as C, and we I look back a BASIC I can see how limited it was, but it certainly was not crappy.

it is a spaghetti code language. it is crappy. and VB is worse because it attempts to add Object layers to Basic but makes it come out as a kludge.
 
  • #9
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.

Someone asks for help with a particular language and the response is to bash the language and its implementations?
 
  • #10
Tide said:
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.
Someone asks for help with a particular language and the response is to bash the language and its implementations?

Here Here!

I have the languages I don't like, but that's only because I find reasonable alternatives for my uses. Nobody writes full scale apps in assembly because its time consuming .. but that would be the most efficient method of code... you have to strike a balance of necessity. If people want to start bashing languages, then I'll go ahead and started opening up some holes .. especially on python lovers.. Object Oriented is good... on paper.. like capitalism, and communism .. in reality it doesn't work well. BASIC was a building block for the IBM .. despite Apple really being a lot more advanced... but.. anyways, BASIC has its uses.. Shell scripting is extremely useful when you use it to do what its meant to do .. same with BASIC... python is meant to make coding easier for nonfunctional programmers ... at least BASIC doesn't consume nasty amounts of resources just to do something simple .
 
  • #11
Tide said:
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.
Someone asks for help with a particular language and the response is to bash the language and its implementations?

The usefulness of a language doe snot mean that it is not crappy.

fact is that Python fits all the situations that Basic and VB can fit and does a better job.
 
  • #12
ComputerGeek said:
The usefulness of a language doe snot mean that it is not crappy.
fact is that Python fits all the situations that Basic and VB can fit and does a better job.

Well, good for you! :)

But can you answer the original poster's questions?
 
  • #13
Wow what a topic this has turned out to be :yuck:
 
  • #14
Thank you very much, my questiosn were all answered between this post and my class, except for the question about the line input prompt. Is that the same as input? If not, what is the difference? Thank you for the assistance. P.S. Whether or not QBasic is the best it is generally seen as the best for beginners, in the second semester I'll be learning Visual Basic, and hopefully next year C++. I might try to take additional classes to learn others at some point also. Even if none are perfect, at leats there is plenty of variety.:smile:
 
  • #15
Whether or not QBasic is the best it is generally seen as the best for beginners, in the second semester I'll be learning Visual Basic, and hopefully next year C++. I might try to take additional classes to learn others at some point also. Even if none are perfect, at leats there is plenty of variety.

You should watch what you say here because certain people may characterize what you are trying to learn as being inferior and will try discourage you.
 
  • #16
Well, at my school they only teach QBasic, Visual Baisc, and C++. At the local college I might take some classes, but I think they only offer C++ and Java. Any others I would need to teach myself (if I took up a separate computer language it would just give me another excuse not to do school work at this point).
 
  • #17
mewhoexactlywhat said:
Well, at my school they only teach QBasic, Visual Baisc, and C++. At the local college I might take some classes, but I think they only offer C++ and Java. Any others I would need to teach myself (if I took up a separate computer language it would just give me another excuse not to do school work at this point).

no offense, but that program is pretty weak. My program had C++, java, Ada, LISP, Assembly (no particular dialect, it was a theoretical Architecture), PHP, Perl, Javascript, Pascal, and a few others.

your program has to expose you to more languages than that. what are the other courses that you have to take?
 
  • #18
ranger said:
You should watch what you say here because certain people may characterize what you are trying to learn as being inferior and will try discourage you.

if he is doing it for a class, then it is not that big of an issue, but come on. stop being politically correct about languages. Would you use SnoBol today? How about B? Basic is not a good language give today's choices and methodology, just as SnoBol is not a good language given todays choices and methodology.
 
  • #19
I'm in high school (also correction: she). It's not a huge school, so there is not much selection with computer languages. Also, I rechecked and the local college also offers some classes in Perl, Linux, and others. However I'm surprised that there was such a selection at ComputerGeek's school. Are you talking about a high school program or a college program?
 
  • #20
mewhoexactlywhat said:
I'm in high school (also correction: she). It's not a huge school, so there is not much selection with computer languages. Also, I rechecked and the local college also offers some classes in Perl, Linux, and others. However I'm surprised that there was such a selection at ComputerGeek's school. Are you talking about a high school program or a college program?

did you mention you were in HS? If so, I glanced over that. in any case, I am talking about a college curriculum which was the frame of reference I was coming from when you started this thread.

I am glad to hear that you are a girl because there are so few girls in Computer science as compared to boys. I am also not as critical now that I know it is a High School program because that is actually a very robust program for a high school. They should however dump QBasic and Visual Basic if their intent is to teach programming. I am not trying to sound like a python evangelist, but as far as kids learning to program, Python enforces best practices, is very easy to learn, and is very powerful. so it covers everything that Qbasic and VB do, plus it teaches good programming techniques. I suggest that you pick up a book about python and learn the language if you are interested.
 
  • #21
I think I'll do that during winter break then. Thank you.:smile:
 

1. What is rounding in computer programming?

Rounding in computer programming is the process of taking a number and adjusting it to a specified precision. This is often done to simplify or make the number more manageable for calculations.

2. How does rounding work in computer programming?

Rounding typically involves using a specific rounding algorithm, such as rounding up or down to the nearest whole number, or rounding to a specified number of decimal places. The algorithm used will depend on the programming language and the specific needs of the program.

3. What is the role of dates in computer programming?

Dates are used in computer programming to represent a specific point in time or a duration of time. They can be used for tasks such as recording events, scheduling tasks, or calculating time intervals.

4. How are dates stored in computer programming?

Dates are typically stored in a specific format, such as year-month-day (YYYY-MM-DD) or month/day/year (MM/DD/YYYY). They can also be stored as a timestamp, which represents the number of seconds or milliseconds since a specific date and time. The specific storage format will depend on the programming language and the needs of the program.

5. What is a line input prompt in computer programming?

A line input prompt is a command or function that prompts the user to input a value or text on a new line. This is often used when creating user input forms or when asking for specific information from the user in a program.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
22
Views
921
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
29
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
11
Views
777
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top