473,654 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decimall Float Question

hey,

I have float values that look something like this when they are
printed:
6.0E-4
7.0E-4

I don't want them to be like this I want them to be normalized with 4
decimal places.

If anybody can show me how to do this it would be greatly appreciated.
Thanks in advance.
-morc

Apr 4 '06 #1
19 4674
// Roundoff routine for 4 decimal places
// used someplaces.

function round(x) {
return Math.round(x*10 000)/10000;
}

Apr 4 '06 #2
thanks
but can somebody guide me on how to do this with BidDecimal.
I've been looking through the documentation and I can't figure it out.

thanks in advance.

Apr 4 '06 #3
"morc" <qu************ *@msn.com> writes:
I have float values that look something like this when they are
printed:
6.0E-4
7.0E-4

I don't want them to be like this I want them to be normalized with 4
decimal places.


If you want to control the representation, you will need to construct
the strings yourself.

One suggestion:
function roundToString(n , decCount) {
var intpart = ((decCount == 0) ? Math.round : Math.floor)(n);
var res = String(intpart) ;
if (decCount > 0) {
var fracPart = n - intpart;
var offset = Math.pow(10,dec Count);
var string = String(Math.rou nd((1 + fracPart)*offse t));
res += "."+string.subs tring(1);
}
return res;
}

It has its limits as well, since it uses Javascript's internal
number-to-string operation at places, but for reasonable sizes
(e.g. 0-10 digits on each site of the decimal points) it should
work.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 4 '06 #4
oh geez... i did not realise i was posting in a javascipt group.
I'm sorry this isn't a java script problem please ignore this post.

Apr 4 '06 #5
JRS: In article <bq**********@h otpop.com>, dated Tue, 4 Apr 2006
19:12:22 remote, seen in news:comp.lang. javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com > posted :

One suggestion:
function roundToString(n , decCount) {
var intpart = ((decCount == 0) ? Math.round : Math.floor)(n);
var res = String(intpart) ;
if (decCount > 0) {
var fracPart = n - intpart;
var offset = Math.pow(10,dec Count);
var string = String(Math.rou nd((1 + fracPart)*offse t));
res += "."+string.subs tring(1);
}
return res;
}

It has its limits as well, since it uses Javascript's internal
number-to-string operation at places, but for reasonable sizes
(e.g. 0-10 digits on each site of the decimal points) it should
work.


Not for negative numbers (try on (0.0 - RoundingError)) ; could be made
to fail better for large positive ones, NaN, infinity, undefined.

The OP should have read the newsgroup FAQ; see below. Section 4.6
treats the topic and has links.

The rounding of exactly-representable half-way cases may need
consideration.

<FAQENTRY> Rename 4.6 to remove the "2", since the article is more
general. </FAQENTRY>

ISTM that fracPart = n % 1

I don't think I have a method using 1+fracpart .. substring(1) in js-
round.htm at present.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 4 '06 #6
el*********@ele ctrician.com said on 05/04/2006 2:45 AM AEST:
// Roundoff routine for 4 decimal places
// used someplaces.

function round(x) {
return Math.round(x*10 000)/10000;
}


Using your function:

round(123.12394 99999999); // gives 123.1239

but:

round(123.12394 999999999); // gives 123.124
Do you know why your function does that?
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
Apr 5 '06 #7
JRS: In article <bq**********@h otpop.com>, dated Tue, 4 Apr 2006
19:12:22 remote, seen in news:comp.lang. javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com > posted :
function roundToString(n , decCount) {
var intpart = ((decCount == 0) ? Math.round : Math.floor)(n);
var res = String(intpart) ;
if (decCount > 0) {
var fracPart = n - intpart;
var offset = Math.pow(10,dec Count);
var string = String(Math.rou nd((1 + fracPart)*offse t));
res += "."+string.subs tring(1);
}
return res;
}

roundToString(1 .994, 2) 1.99
roundToString(1 .996, 2) 1.00

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/>
Apr 6 '06 #8
Dr John Stockton <jr*@merlyn.dem on.co.uk> writes:
roundToString(1 .994, 2) 1.99
roundToString(1 .996, 2) 1.00


Whoops. Just ignore that an I'll try again. :)

function roundToString(n umber, positions) {
var e = Math.pow(10,pos itions);
var s = String(Math.rou nd(number*e));
var offset = s.length - positions;
return s.substring(0,o ffset) + "." + s.substring(off set);
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 6 '06 #9
Lasse Reichstein Nielsen said on 07/04/2006 6:46 AM AEST:
Dr John Stockton <jr*@merlyn.dem on.co.uk> writes:

roundToString(1 .994, 2) 1.99
roundToString(1 .996, 2) 1.00

Whoops. Just ignore that an I'll try again. :)

function roundToString(n umber, positions) {
var e = Math.pow(10,pos itions);
var s = String(Math.rou nd(number*e));
var offset = s.length - positions;
return s.substring(0,o ffset) + "." + s.substring(off set);
}

/L

That still has issues, e.g.:

roundToString(1 .015, 2) 1.01
roundToString(1 .025, 2) 1.02
roundToString(1 .035, 2) 1.03
roundToString(1 .045, 2) 1.05
roundToString(1 .055, 2) 1.06
It seems to me that the only way to properly round numbers is to treat
them as a string and round as a human would: to round to n places, look
at the n+1 digit and round right-to-left from there.

The use of Numbers and any kind of decimal arithmetic introduces inaccuracy.
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
Apr 6 '06 #10

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

Similar topics

5
5506
by: Code4u | last post by:
In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The problem I ran into is a float with value INT_MAX assigned to an int results in the value -2147483648 being assigned, but if the conversion takes place in an expression INT_MAX is assigned as I would expect: int...
15
12811
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 last):
6
2290
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double (*f)(float)" But I want to be able to pass a
3
1762
by: hantechs | last post by:
<html> <body> <p style="width:30%;">text1</p> <p style="float:left;">text2</p> </body> </html> The effect of this html code is : text1 and text2 each is on a line. My question is: Why text2 is positioned on the right of text1? Because the CSS2.1 said: A floating element must be placed as high as possible; A left-floating element must be put as far to the left as possible; A
4
1741
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good design to have large objects with several has-a relationship with other objects. Second, I want my unit to have a coord struct. struct coord{ int x;
9
3397
by: gdarian216 | last post by:
I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class. I just dont know how to get started and if I will have to re-write my whole program. #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std;
12
4024
by: kostas | last post by:
Hi I was asked to propose an interview question for C/C++ programmers (CAD/CAE software) I came up with the following ---------------------------------------------------------------------------------------- float fun(float value) { float f1 =0., f2 = value; float tol = value/1000.; float result,tmp;
22
2761
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
998
by: Timothy Grant | last post by:
That's because s IS a string. It's not been converted to a float. In : s = '3.1415' In : n = float(s) In : type(s) Out: <type 'str'> In : type(n) Out: <type 'float'> Why are you avoiding the very simple try:/except: solution to this problem?
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.