PDA

View Full Version : converting time to whole numbers


roihandicapping
02-25-2009, 02:41 PM
Please help me with the following

Hi All, I brand new to programming and i'm trying to write a program (javascript:void(0)) to convert decimals to whole numbers. 46.0 is 46 seconds of one minute

I want to convert decimal to whole numbers.

What is the code for entering "46.0" in TextBox1.Text and getting a result of "100" in TextBox2.Text?



45.0=105
45.1=104
45.2=103
45.3=102
45.4=101
46.0 = 100
46.1= 99
46.2= 98
46.3= 97
46.4=96


Here is my code. What am I doing wrong?


TextBox2.Text = TextBox2.Text

TextBox1.Text = Convert.ToDecimal(TextBox1.Text)

If TextBox1.Text = "46.0" Then

TextBox2.Text = "100"

TextBox1.Text = TextBox2.Text

End If

DJofSD
02-25-2009, 03:57 PM
Textbox1.Text is a string. When you assign the result of Convert.ToDecimal to a string variable, I would expect either a cast (conversion) or an error.

roihandicapping
02-25-2009, 04:10 PM
All i want to do is enter 46.0 in textboxA and have it give me the number "100" in textboxB


What is the code?

robert99
02-25-2009, 05:23 PM
Unless you tell us the error message it could be all sorts of things.
You may find the parseInt function does what you want if you really want an integer.

You do not really need to covert to an integer as your text is "100" which is a string. So cross out your second line.


Convert a String to an int
To convert a String to an int, call the static method parseInt() on the Integer class.

String string = "123";
int value = Integer.parseInt(string);

See also:

http://blog.codebeach.com/2008/03/convert-string-to-number-in-java.html

http://forums.sun.com/thread.jspa?threadID=5182868&tstart=38115

http://bytes.com/groups/net-c/488526-making-convert-todecimal-adapt-language-settings

CincyHorseplayer
02-25-2009, 05:49 PM
I'm having the opposite problem.

Raybo helped me write up a simple pace program last NOvember and I haven't used it til last Saturday.

Excel is converting everything into whole numbers.It's rounding up or down to 46 or 23 from say 45.6 or 23.2.Should I just shrink the font to fit the box or what??

Ray or anybody,if you could help me with this I'd be totally grateful.I was pissed when I sat down late Saturday morning ready to put some Tampa numbers through it and couldn't.

Jeff P
02-25-2009, 06:15 PM
Here's the source code for a simple html document:

<title>Enter Time and Click Calc</title>
<html>
<head>
<script language="javascript">
<!--


function convertimetonumber()
{

var time = document.getElementById('time').value

document.getElementById('rating').value = "";


if (time >= 44)
{
document.getElementById('rating').value = 105;
}

if (time >= 44.1)
{
document.getElementById('rating').value = 104;
}

if (time >= 44.2)
{
document.getElementById('rating').value = 103;
}

if (time >= 44.3)
{
document.getElementById('rating').value = 102;
}

if (time >= 44.4)
{
document.getElementById('rating').value = 101;
}

if (time >= 45)
{
document.getElementById('rating').value = 100;
}

}



function reset()
{
document.getElementById('time').value = "";
document.getElementById('rating').value = "";

}

-->
</script>
</head>
<body>


<b>Enter fractional time in seconds and fifths and click the Calc button:</b>
<br><br>


Enter Time:
<input name="time" id="time" type=text maxlength=50 style="width:220px;" size="20">


Power Rating:
<input name="rating" id="rating" type=text maxlength=50 style="width:220px;" size="20">

<br><br>
<input type="button" onclick="javascript: convertimetonumber();">Calc</input>

<br><br>
<input type="button" onclick="javascript: reset();">Reset</input>

</body>
</html>

Paste it into Notepad and then save it to disc - giving it a file extension of .html. Then navigate to the folder where you saved it, give the file a double click - and it should pop open in your browser.


The file has two textboxes. The first has a name and id of "time" the purpose of which is for user input of raw time. The second box has an id of "rating" - the purpose of which is to display ratings converted from time input into the first box by the user.

Time is converted to rating using a javascript function named converttimetonumber() - The function reads the value of fractional time input by the user into the "time" box, makes the conversion through a stepwise series of javascript if-then statements, and then displays the rating in the "rating" box. The function is called in the onclick event of the Calc button. There is also a Reset button that clears the content of both boxes.

Enjoy,


-jp

.

cj
02-25-2009, 08:53 PM
TextBox2 = 330-INT(TextBox1)*5 - MOD(TextBox1*10,INT(A1))

I don't know java syntax off the top of my head, but MOD is the remainder and INT is the integer part of the number. I'm sure the functions MOD and INT are available so that is one line of code that does what you want if I'm not mistaken.

cj
02-25-2009, 10:04 PM
TextBox2 = 330-INT(TextBox1)*5 - MOD(TextBox1*10,INT(TextBox1))

I don't know java syntax off the top of my head, but MOD is the remainder and INT is the integer part of the number. I'm sure the functions MOD and INT are available so that is one line of code that does what you want if I'm not mistaken.

Slight typo, fixed with bold above. The above was done in Excel, but the gist of it is correct. The 330 would be the rating for the time of 0.0.

The syntax for the last part would look different with most languages. For example, C++ would be:

(TextBox1*10) % INT(TextBox1)

cj
02-27-2009, 11:34 AM
Did I get this wrong, or did I just waste 20 minutes of my life typing it up for the thread starter?

raybo
02-27-2009, 06:40 PM
I'm having the opposite problem.

Raybo helped me write up a simple pace program last NOvember and I haven't used it til last Saturday.

Excel is converting everything into whole numbers.It's rounding up or down to 46 or 23 from say 45.6 or 23.2.Should I just shrink the font to fit the box or what??

Ray or anybody,if you could help me with this I'd be totally grateful.I was pissed when I sat down late Saturday morning ready to put some Tampa numbers through it and couldn't.

Rod,

If there is room for the decimal portion, in the cells, but they are not displaying then you need to right click the cell, select "Format cells", on the "Number" tab select "Number" then choose the number of decimal places you want.

If the decimals are there but the cells are not wide enough to display the decimals then you can either widen the column that contains the cells in question, or reduce the font size. To widen the column, right click the column header, select "Column Width" and then increase the column width size until the decimals are displayed. Another way is to move your cursor to the right border of the column header, the cursor will change to left and right arrows, left click the right border of the column header and slide to the right until the decimals appear.