Sponsored Content
Top Forums Web Development Javascript to force decimals in 0.25 steps Post 302918988 by Corona688 on Friday 26th of September 2014 01:53:35 PM
Old 09-26-2014
Quote:
Floating point does not always represent some numbers precisely
0.5 and 0.25 are simple binary fractions of 1, floating point will represent it perfectly as long as it has 2+ bits of fraction. For 32-bit floats, anything inside +/-2 million will work perfectly.

Computationally speaking, this is the most direct method: Multiply by 4, strip off decimals, divide by 4.
Code:
Math.floor(var*4) / 4


Last edited by Corona688; 09-26-2014 at 03:04 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

handle decimals

Hi All, How we can handle decimals in (Float) in UNIX. a=73 b=5 c=`expr a / b` i am getting 14 but i need full 14.6 . Can any one help me pls? (1 Reply)
Discussion started by: subin_bala
1 Replies

2. Shell Programming and Scripting

Bourne and decimals??

I need to get 15% of the variable exer1 to be added to other exercises so far, i've got exer1=$1 aver=`expr $exer \* .15` but i keep getting an error that an integer value was expected. Is there anyway around this? (1 Reply)
Discussion started by: kdyzsa
1 Replies

3. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

4. Shell Programming and Scripting

Decimals in TCSH

Hello, I want to run a loop with non-integer values (which I know I can't) so I've created a loop of integers and divided it by 10. However, these values are always rounded down to 1 significant figure. How do I get the script to keep and use the decimal value? My script is as follows #... (1 Reply)
Discussion started by: DFr0st
1 Replies

5. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

6. Shell Programming and Scripting

Grep wholenumbers / decimals

Hi I have a large file in which I need to search for certain whole numbers and print the whole line. I'm currently trying this via command line using grep but grep is also matching the decimal values and i just want to return the matching whole numbers (the entire line) Example File ddggg ... (7 Replies)
Discussion started by: duckeggs01
7 Replies

7. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

8. Shell Programming and Scripting

Getting date in seconds with decimals

I am trying to get date to display decimal Desired output 1350386096256.12 I know this can be done with printf, but are not able to make it work. I have tested this and many otherprintf "%.2f" $(($(date +%s%N)/1000000)) (8 Replies)
Discussion started by: Jotne
8 Replies

9. Programming

Decimals reading Python

Hello, i'm new in python. Consider that i have this function that read me some data from a serial : def GetData(): line = open(serialx).read() hash = line.find("#") when = line count = line # print when, count, line return (float(when), int(count)) it gives me the result... (2 Replies)
Discussion started by: Board27
2 Replies

10. Shell Programming and Scripting

how to find the field has more than 2 decimals

Hi Gurus, I have below sample file, I need find the line which 2rd field has more than 2 decimals. in sample file, I need to find xyz, 123456.789 abc, 1234.45, def xyz, 123456.789, xxx bce, 1234.34, xxx thanks in advance (13 Replies)
Discussion started by: ken6503
13 Replies
Number::Fraction(3pm)					User Contributed Perl Documentation				     Number::Fraction(3pm)

NAME
Number::Fraction - Perl extension to model fractions SYNOPSIS
use Number::Fraction; my $f1 = Number::Fraction->new(1, 2); my $f2 = Number::Fraction->new('1/2'); my $f3 = Number::Fraction->new($f1); # clone my $f4 = Number::Fraction->new; # 0/1 or use Number::Fraction ':constants'; my $f1 = '1/2'; my $f2 = $f1; my $one = $f1 + $f2; my $half = $one - $f1; print $half; # prints '1/2' ABSTRACT
Number::Fraction is a Perl module which allows you to work with fractions in your Perl programs. DESCRIPTION
Number::Fraction allows you to work with fractions (i.e. rational numbers) in your Perl programs in a very natural way. It was originally written as a demonstration of the techniques of overloading. If you use the module in your program in the usual way use Number::Fraction; you can then create fraction objects using "Number::Fraction-"new> in a number of ways. my $f1 = Number::Fraction->new(1, 2); creates a fraction with a numerator of 1 and a denominator of 2. my $f2 = Number::Fraction->new('1/2'); does the same thing but from a string constant. my $f3 = Number::Fraction->new($f1); makes $f3 a copy of $f1 my $f4 = Number::Fraction->new; # 0/1 creates a fraction with a denominator of 0 and a numerator of 1. If you use the alterative syntax of use Number::Fraction ':constants'; then Number::Fraction will automatically create fraction objects from string constants in your program. Any time your program contains a string constant of the form "d+/d+" then that will be automatically replaced with the equivalent fraction object. For example my $f1 = '1/2'; Having created fraction objects you can manipulate them using most of the normal mathematical operations. my $one = $f1 + $f2; my $half = $one - $f1; Additionally, whenever a fraction object is evaluated in a string context, it will return a string in the format x/y. When a fraction object is evaluated in a numerical context, it will return a floating point representation of its value. Fraction objects will always "normalise" themselves. That is, if you create a fraction of '2/4', it will silently be converted to '1/2'. Experimental Support for Exponentiation Version 1.13 of Number::Fraction adds experimental support for exponentiation operations. If a Number::Fraction object is used as the left hand operand of an exponentiation expression then the value returned will be another Number::Fraction object - if that makes sense. In all other cases, the expression returns a real number. Currently this only works if the right hand operand is an integer (or a Number::Fraction object that has a denominator of 1). Later I hope to extend this so support so that a Number::Fraction object is returned whenever the result of the expression is a rational number. For example: '1/2' ** 2 # Returns a Number::Fraction ('1/4') '2/1' ** '2/1' Returns a Number::Fraction ('4/1') '2/1' ** '1/2' Returns a real number (1.414213) 0.5 ** '2/1' Returns a real number (0.25) METHODS
import Called when module is "use"d. Use to optionally install constant handler. unimport Be a good citizen and uninstall constant handler when caller uses "no Number::Fraction". new Constructor for Number::Fraction object. Takes the following kinds of parameters: o A single Number::Fraction object which is cloned. o A string in the form 'x/y' where x and y are integers. x is used as the numerator and y is used as the denominator of the new object. o Two integers which are used as the numerator and denominator of the new object. o A single integer which is used as the numerator of the the new object. The denominator is set to 1. o No arguments, in which case a numerator of 0 and a denominator of 1 are used. Returns "undef" if a Number::Fraction object can't be created. to_string Returns a string representation of the fraction in the form "numerator/denominator". to_num Returns a numeric representation of the fraction by calculating the sum numerator/denominator. Normal caveats about the precision of floating point numbers apply. add Add a value to a fraction object and return a new object representing the result of the calculation. The first parameter is a fraction object. The second parameter is either another fraction object or a number. mult Multiply a fraction object by a value and return a new object representing the result of the calculation. The first parameter is a fraction object. The second parameter is either another fraction object or a number. subtract Subtract a value from a fraction object and return a new object representing the result of the calculation. The first parameter is a fraction object. The second parameter is either another fraction object or a number. div Divide a fraction object by a value and return a new object representing the result of the calculation. The first parameter is a fraction object. The second parameter is either another fraction object or a number. exp Raise a Number::Fraction object to a power. The first argument is a number fraction object. The second argument is another Number::Fraction object or a number. If the second argument is an integer or a Number::Fraction object containing an integer then the value returned is a Number::Fraction object, otherwise the value returned is a real number. abs Returns a copy of the given object with both the numerator and denominator changed to positive values. EXPORT None by default. SEE ALSO
perldoc overload AUTHOR
Dave Cross, <dave@mag-sol.com> COPYRIGHT AND LICENSE
Copyright 2002-8 by Dave Cross This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-11-26 Number::Fraction(3pm)
All times are GMT -4. The time now is 02:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy