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

Calculating decimals

132 100+
Hi guys,

I've been trying to write script that counts decimals. I know Javascript won't work with them so you have to bypass it.

I've looked around on the net and it seems that most people are using the match.pow() method. However I don't understand why? But then again I was never good at Math. Considering I flunked every year :-)

The only thing I want to do is add. So if I have two times 2.48 I would like it to show 4.96. Now it just gives me 4.

If anyone could clear it up for me, that would be nice.
Jun 9 '08 #1
3 1492
mrhoo
428 256MB
If your numbers are coming from user input, make sure they are numbers:
number=parseFloat(string) or Number(string)

You can return the number as a string formatted to the number of decimals you want by returning (number).toFixed(2) or (number).toFixed(3)

You can force 3 decimal accuracy by multiplying your number by 1000 and dividing the integer part by the same.

Math.floor(1.235666*1000)/1000 returns the number 1.235
(1.235666).toFixed(3) returns the string '1.235'

Math.pow(10,n) return 10 to the nth power.
Jun 9 '08 #2
gits
5,390 Expert Mod 4TB
... I know Javascript won't work with them so you have to bypass it. ...
mrhoo already gave you a very good answer ... and as you should see ... JavaScript WORKS with them and you DON'T need to bypass anything. like every programming language JavaScript has the common primitive data types like strings, booleans and even numbers that could be integers or floats :)
i think what you really mean is that every user input that comes from a webpage's form is first considered a string and you should type-cast it to use it as a number. some operations do implicit type-casts but the better way would be to cast explicitly before any operation so that you could know what data you have to process. while javascript don't declare types of variables it is definitly important what type a concrete instance of a variable is ... so you couldn't multiply 2 strings but you could multiply 2 numbers ;)

kind regards
Jun 9 '08 #3
Cainnech
132 100+
Thanks guys,

You cleared things up for me.
I now also know why my script wouldn't work. I was using parseInt, but ofcourse silly me forgot that this is only for whole numbers and not for decimals, so thank you Magoo for pointing out Number() to me.

And a second problem I had was that my decimals were divided by a "," and not by a "." which resulted in NaN. It took me a while to figure that out but in the end everything works !
:-)

Greets,

Cainnech
Jun 9 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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?

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.