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

Rounding to two digit in javascript

121 100+
Hello,
This seems to be very common question, but my problem hasn't been addressed anywhere.

My code look like
Expand|Select|Wrap|Line Numbers
  1. var A = Math.round(B * Math.pow(10,2))/Math.pow(10,2);
Now the code works absolutely fine if B is 1.2345.It gets changed to 1.23 (2 digits, hunky dory)

problem starts if B is 1 , I need to display 1.00 instead of 1 as i am assigning this variable A value to some textbox and just for the sake of consistency we need to show every thing upto two decimal places.

I have googled for 2 hours and everyone shows how to round a complex decimal number to 2 decimal points but i haven't come across any webpage showing the solution for my problem.

Any help will be appreciated.

Jay
Sep 27 '10 #1

✓ answered by Dormilich

it should work then with .toFixed(2)

4 2963
Dormilich
8,658 Expert Mod 8TB
er, B.toFixed(2) or B.toPrecision(2)?
Sep 27 '10 #2
jay123
121 100+
didnt work for my problem Dormilich, ok my whole problem is:

I have a page with a number(A) displayed as label ,Textbox(B) and Button

When one click on button it adds A and B and sum is displayed in textbox(C). Now thats where i want to display data to 2 digit . I tried your toPrecision(2) and in Textbox C it shows 2.3e+3, instead of 2318 (where A(Label) was 2317.28 and i entered in .72 in B)
Sep 27 '10 #3
Dormilich
8,658 Expert Mod 8TB
it should work then with .toFixed(2)
Sep 27 '10 #4
jay123
121 100+
Worked like a charm, Thanks
Sep 28 '10 #5

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
3
by: Ricardo Garcia | last post by:
hi, i have some file size in bytes, and i want to get it in KB or MB, with one decimal digit, for example: 1268777 -> 1.2 MB now i can have 1.210000... my question is how do you can round this...
1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
16
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, I have a neat little script that calculates price based on quantity without refreshing the page.. the script is - <script type="text/javascript"> function OpenWin(url) {...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.