How to make it end after one decimal but keep two columns? - decimal

The question is asking us to keep it in two columns not in a row of lines.
I tried the whole System.out.printf("%.2f") but like I said, it makes the numbers come out in a row and not in a column.
Please try to keep it beginner level because this is only my 2nd CompSci class.
Thank you!
System.out.println("Kilograms Pounds");
final double pounds = 2.2;
for (int kilo = 1; kilo < 200; kilo += 2){
System.out.println(kilo + " " + (kilo*pounds));
}

It's very hard to know what you're doing or what you want, but assuming you're using Java 1.5 or later, and assuming you want output like:
Kilograms Pounds
1 2.2
3 6.6
5 11.0
...
then easiest to stick with printf then trying to add formatters to println. Just append \n to your strings to add new lines. For example:
System.out.println("Kilograms Pounds");
final double pounds = 2.2;
for (int kilo = 1; kilo < 200; kilo += 2)
{
System.out.printf("%d %.1f\n", kilo, kilo*pounds);
}

Like other people have stated it's not 100% clear what you are trying to achieve based on what you posted but I believe what you want is the output as
Kilograms Pounds
1 2
3 6
5 11
7 15
Try this:
System.out.println(kilo + " " + (int) Math.floor(kilo*pounds)); }
It will round your expression then it using the int cast it will cut of the decimal as you state in your question header.

Related

Anylogic Excel Row in Parameter

I have an Excel File which I like to use to fill my parameter. The Excel file is an output so I don't want to change something on the formation of the file.
For my first parameter I want to import all values of the certain row in the Excel file. For example all values for all years of row 3. I already created a parameter as army with the years as dimension but doesn't know how to get the values in the best way. Maybe someone can help me.
Assume number of years equal to 30 and you have an array p[] with years as dimension:
for( int i = 0 ; i < 30 ; i++)
{
p[i] = excelFile.getCellNumericValue(1, 3, i + 3);
i++;
}

Find numbers in file 2 within a specified range in 1 file and then calculate the average value of another column within that range

I've been trying to work this one out for weeks and I know it's a simple awk script, but I can't seem to make much headway. Any thoughts?
I have two .txt files that I am dealing with.
File 1 (Column 1 has the same value)-
1 1000 1
1 1003 3
1 1020 6
1 167999 5
1 167222 4
File 2 (Column 1 has the same value)-
1 1000 1050
1 167000 168000
I want to search for values in column 2 of file 1 that fall within the range specified in columns 2 and 3 of file 2. Then I want to find the average value of column 3 in file 1 within the now specified ranges, and output the list of average values. Any help is much appreciated as I am still new to the coding world!
Here's a snippet that should help you get started:
awk -v rangefile=file2 '
BEGIN {
ranges=0
while((getline < rangefile) > 0) {
++ranges
low[ranges] = $2
high[ranges] = $3
}
}
{
for(i = 1; i <= ranges; i++) {
if($2 >= low[i] && $2 <= high[i]) {
...
}
}
}
END {
for(i = 1; i <= ranges; i++) {
print low[i], high[i]
}
}
' file1
I didn't fill in the ... part, but I'm hoping the above covers the tricky part. For ..., you could just use arrays for the sum and count similar to the arrays for low and high. In the END block, you would repeat the for(i...) loop, and inside that loop print out whatever you wanted your output format to be.

Excel; how to combine left and right functions

I have a column of text values that I want to delete the first 8 and last 4 characters from i.e '12345678john1234' would just become 'john'. I know how to use the left and right functions separately but can't seem to get them working together. The mid function won't work in this case. Is this possible? Thanks
Dear Use this formula and you can get your answer
=LEFT(RIGHT(A1,8),4)
Problem solved with:
=MID(a1,9,LEN(a1)-12)
Explaination:
MID: get the middle string
=MID(text,start_nums,numchars)
start_nums = 9 to start from the character j
numchars = len(text)-12 = 16 - 12 = 4
Then you will get the result as john

deviding an even and an uneven number

I am very happy to discoverd this site. I get very good help. Hope you guys can help me with another problem. I want to round a number. Lets say I have a number 39 if I devide this into 2 then I get 18.5. Which makes very logical. But when you are counting in persons then you can't cut a person in half. So I am looking for a formule in vba. I tried the round function
group=39
devideby = 3 'devideby can be 2 or 3
test = round(group/devideby)
If I do this I get test=20. I want to have 2 or 3 separate answers: if devided by the number 2 then I want to have 20 and 19. If devided by 3 then I want to have 13, 13 and 13. Is there a way to solve this?
You could try something like this:
Dim arrOutput(2)
Select Case divideby
Case 2
arrOutput(0) = Int(group / divideby)
arrOutput(1) = group - arrOutput(0)
arrOutput(2) = 0
Case 3
arrOutput(0) = Int(group / divideby)
arrOutput(1) = Int((group - arrOutput(0)) / 2)
arrOutput(2) = (group - arrOutput(0)) - arrOutput(1)
Case Else
MsgBox "Error"
End Select
It's not very elegant but I think it does what you are asking

How to column-ify an output from a certain program?

I have a program that generates and outputs a sequence of simple sample math homework tasks, like:
1 + 1 = ...
3 + 3 = ...
2 + 5 = ...
3 + 7 = ...
4 + 2 = ...
a sequence can be quite long, and I'd like to save space when this sequence is printed by converting it as follows:
1 + 1 = ... 3 + 7 = ...
3 + 3 = ... 4 + 2 = ...
2 + 5 = ...
that is, wrapping the lines into the two or more columns. I was expecting the column linux utility to do the job using the -c N option witn N=2, however, it still outputs the lines in one column whatever the N is.
How would I do the column-ifying of the sequence of lines?
Believe it or not, the utility you want is pr, not columns. If you want your file turned into 3 columns:
pr -3 textfile.txt
If you want to fill in rows first, then columns:
pr -l1 -t -3 textfile.txt
I got these sample invocations from the ever-so-useful UNIX Power Tools. This is Recipe 21.16.
The -c parameter to column is used to specify the number of "columns" your display has, counted in characters. The column tool then figures out the number of "output columns" that fits in the given number of "character columns". Passing a small number of "character columns" will almost always yield a single "output column", because more won't fit in the given number of characters. There does not seem to be a way to pass the number of ouput columns on the command line.

Resources