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

Truncate a decimal pointed figure without rounding ...

How can I get -0.162058 to -0.1 rather than rounding to -0.2?

I know I'm being stupid here, but it's really bugging me. The number before
the decimal point could 0 - 999

Thanks
Jul 17 '05 #1
4 13992
elyob wrote:
How can I get -0.162058 to -0.1 rather than rounding to -0.2?

I know I'm being stupid here, but it's really bugging me. The number before
the decimal point could 0 - 999


Something like this?

<?php
function number_trim($x) {
if ($x<0)
return ceil($x);
else
return floor($x);
}

$x = -0.162058;
echo number_trim($x*10), "\n";
?>

--
..sig
Jul 17 '05 #2
Hello,

elyob wrote:
How can I get -0.162058 to -0.1 rather than rounding to -0.2?
I know I'm being stupid here, but it's really bugging me. The number
before the decimal point could 0 - 999


After making a function myself that was just far more complex
than needed, I saw in the user contributed notes of the manual
that someone contributed a suggestion for such function :

jbeardsl [found_at] gte [d0t] net
08-Nov-2002 01:15
I was looking for a truncate function. Not finding one, I wrote my own.
Since it deals with everything as a number, I imagine it's faster than the
alternative of using string functions. HTH...

function truncate ($num, $digits = 0) {

//provide the real number, and the number of
//digits right of the decimal you want to keep.

$shift = pow(10, $digits);
return ((floor($num * $shift)) / $shift);
}

Best regards,

Eric
Jul 17 '05 #3

"Eric Veltman" <eric@[RemoveThis]veltman.nu> wrote in message
news:vq************@corp.supernews.com...
Hello,

elyob wrote:
How can I get -0.162058 to -0.1 rather than rounding to -0.2?
I know I'm being stupid here, but it's really bugging me. The number
before the decimal point could 0 - 999


After making a function myself that was just far more complex
than needed, I saw in the user contributed notes of the manual
that someone contributed a suggestion for such function :

jbeardsl [found_at] gte [d0t] net
08-Nov-2002 01:15
I was looking for a truncate function. Not finding one, I wrote my own.
Since it deals with everything as a number, I imagine it's faster than the
alternative of using string functions. HTH...


<snip>

Couldn't get this to work. In the end I exploded by the "." and substr'd the
[1]

Thanks for help
Jul 17 '05 #4

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bo*************@ID-203069.news.uni-berlin.de...
elyob wrote:
How can I get -0.162058 to -0.1 rather than rounding to -0.2?

I know I'm being stupid here, but it's really bugging me. The number before the decimal point could 0 - 999


Something like this?

<?php
function number_trim($x) {
if ($x<0)
return ceil($x);
else
return floor($x);
}

$x = -0.162058;
echo number_trim($x*10), "\n";
?>


Didn't seem to want to work for me ... found a different method (as in other
reply).

Thanks
Jul 17 '05 #5

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
2
by: RC | last post by:
I am getting input from a digital scale and if the weight on the scale is 9.25 pounds, the input looks like: 009.25LB I need to remove the LB and the leading zeros and have the remaining value...
1
by: | last post by:
eg. "19.121" to 19.12 decimal eg. "12.567 " to 12.56 decimal
2
by: Thor W Hammer | last post by:
How can we format a decimal variable so that it has two decimal digits but not rounding it. Like this: Original value: 5.225; Output: 2.22; and NOT 2.23
3
by: mlafarlett | last post by:
In the example below, the 'convert.ToDecimal' removes the trailing 5. I'd like to get the number, in tact, moved to the decimal variable. Any help would be greatly appreciated. Dim dcml As...
248
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) ...
1
by: chinni0719 | last post by:
Can you plz tell me how to truncate zeros after decimal the case is like this round(992.55,0) result =993.00 round(993.888) result = 994.000
5
by: bodycode | last post by:
I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level. I want...
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...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.