473,386 Members | 1,779 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,386 software developers and data experts.

make a float from two integers

Ike
If I have two variables that contain integer values, and I want the second
one to actually be the decimal portion of a single, floating-point number),
e.g. 3 and 7 to be therefore 3.7, how can I do this, if I don;t know in
advance how many significant digits the 'second' number will have? That is,
I do not know if I will have, say, 3 and 7 for 3.7, or 8 and 48501 for
8.48501.

It seems there must be a way if I treat these as String values, but I don;t
really see how to do this. Thanks, Ike
May 20 '06 #1
5 1753
"Ike" <rx*@hotmail.com> writes:
If I have two variables that contain integer values, and I want the second
one to actually be the decimal portion of a single, floating-point number),
e.g. 3 and 7 to be therefore 3.7, how can I do this, if I don;t know in
advance how many significant digits the 'second' number will have?
You can't. How would you represent 4.06?
That is, I do not know if I will have, say, 3 and 7 for 3.7, or 8
and 48501 for 8.48501.
or 3 and 7 for 3.07 or 3.007.

You would be better off if you reversed the second number, i.e.,
32 and 45 became 32.54. Then you could have 100 and 200 becomming
100.002, and you could represent all numbers in the possible range.
It seems there must be a way if I treat these as String values, but I don;t
really see how to do this.


You could convert them to strings, but you would already have lost the
necessary information, because a number value cannot have prefixed zeros,
and you need this.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 20 '06 #2


Ike wrote:
If I have two variables that contain integer values, and I want the second
one to actually be the decimal portion of a single, floating-point number),
e.g. 3 and 7 to be therefore 3.7, how can I do this, if I don;t know in
advance how many significant digits the 'second' number will have? That is,
I do not know if I will have, say, 3 and 7 for 3.7, or 8 and 48501 for
8.48501.


One possible way:

var n1 = 8;
var n2 = 48501;
var n3 = parseFloat(String(n1) + '.' + String(n2));

--

Martin Honnen
http://JavaScript.FAQTs.com/
May 20 '06 #3
JRS: In article <44***********************@newsread4.arcor-online.net>,
dated Sat, 20 May 2006 15:45:55 remote, seen in
news:comp.lang.javascript, Martin Honnen <ma*******@yahoo.de> posted :
Ike wrote: Please give informative attributions, as preferred by USEFOR. You're
not posting in a German newsgroup here.
If I have two variables that contain integer values, and I want the second
one to actually be the decimal portion of a single, floating-point number), ^^^^^^^
You mean that it represents the fractional part. In Javascript, all
Numbers are IEEE Doubles, which are binary floats - see FAQ.
e.g. 3 and 7 to be therefore 3.7, how can I do this, if I don;t know in
advance how many significant digits the 'second' number will have? That is,
I do not know if I will have, say, 3 and 7 for 3.7, or 8 and 48501 for


You can do that; but you will be unable to represent a non-integer with
a zero following its decimal point.
One possible way:

var n1 = 8;
var n2 = 48501;
var n3 = parseFloat(String(n1) + '.' + String(n2));


or n3 = +(n1 + '.' + n2)
or n3 = n1 + n2/Math.pow(10, String(n2).length)

--
© 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.
May 21 '06 #4
Martin Honnen wrote:
One possible way:

var n1 = 8;
var n2 = 48501;
var n3 = parseFloat(String(n1) + '.' + String(n2));


With the ',' operand, the other operands of the concatenation are
implicitly converted to string, so String() is not necessary.

var n3 = parseFloat(n1 + '.' + n2);
PointedEars
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one.
-- Benjamin Franklin
May 25 '06 #5
JRS: In article <14****************@PointedEars.de>, dated Thu, 25 May
2006 13:05:32 remote, seen in news:comp.lang.javascript, Thomas 'House-
Painter' Lahn <Po*********@web.de> posted :
Martin Honnen wrote:
One possible way:

var n1 = 8;
var n2 = 48501;
var n3 = parseFloat(String(n1) + '.' + String(n2));


With the ',' operand, the other operands of the concatenation are
implicitly converted to string, so String() is not necessary.

var n3 = parseFloat(n1 + '.' + n2);


You're posting too hastily, as usual, and not checking what you write.

There is no ',' operand.

Using parseFloat is unnecessary. See FAQ 4.21.

Your signature remains non-compliant with FYI28; fix it.
--
© 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.
May 25 '06 #6

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

Similar topics

8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
9
by: PengYu.UT | last post by:
Hi, I write the content of a in file "data" (in Sun Machine). Then I read "data" in both SunOS and linux. But the result is different. Do you know how to make it binary data portable. Best...
5
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
15
by: Kay Schluehr | last post by:
I wonder why this expression works: >>> decimal.Decimal("5.5")**1024 Decimal("1.353299876254915295189966576E+758") but this one causes an error 5.5**1024 Traceback (most recent call...
13
by: kennethlou | last post by:
Hi, If in C a variable appears like X=10.000000, I can round it to zero decimal places. ie X=10? I then have to save the number into a variable. The method appears below:...
11
by: redefined.horizons | last post by:
I'm still pretty new to Python. I'm writing a function that accepts thre integers as arguments. I need to divide the first integer by te second integer, and get a float as a result. I don't want...
16
by: DirtyHarry | last post by:
Good day everyone. This sounds like a stupid question, but I became just curious yesterday, and I looked up several textbooks. However, no textbooks on computer language (that I have ) mentioned...
8
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val =...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.