I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command. Ei,
x=.13241414515
y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the
print statement, you can do something like %.4d, but how can I do this
with converting the number to a string? Thanks! 11 1276
Tuvas wrote: I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command. Ei,
x=.13241414515 y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the print statement, you can do something like %.4d, but how can I do this with converting the number to a string? Thanks!
%d for a floating-point type? Is that right?
Anyway, ITYW:
"%.4g" % x
E.g: x=.13241414515 y="%.4g something here" % x print y
0.1324 something here
I think you answered your own question. :)
x = 0.12345678
y = "%.4f something here" % x
On Wed, 2005-11-09 at 11:52 -0800, Tuvas wrote: I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command. Ei,
x=.13241414515 y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the print statement, you can do something like %.4d, but how can I do this with converting the number to a string? Thanks!
Yep, I was thinking in C, not python. Thanks for the help!
On 2005-11-09, Tuvas <tu*****@gmail.com> wrote: I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command.
Sorry, that's not possible.
x=.13241414515 y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the print statement, you can do something like %.4d, but how can I do this with converting the number to a string?
I don't understand what you mean about the print statement.
If using str() isn't really a requirement, you can use the
string formatting operator "%" like this: x=.13241414515 y = "%0.4g" % x y
'0.1324'
--
Grant Edwards grante Yow! A GRAM?? A BRAM... A
at GROOM... A BROOM... Oh,
visi.com Yeh!! Wash the ROOM!!
Wait, one more question. If the number is something like:
1.32042
It is like
"1.32 stuff"
I would like it's size to remain constant. Any way around this?
"Tuvas" <tu*****@gmail.com> wrote: I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command. Ei,
x=.13241414515 y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the print statement, you can do something like %.4d, but how can I do this with converting the number to a string? Thanks!
you mean "%.4g" ? the % operator is a string operator, and can be used
outside print:
text = "%.4g" % value
for more details, here's the first google hit for "python string formatting": http://docs.python.org/lib/typesseq-strings.html
but that doesn't make too much sense if you don't know how things work
in C, which is explained here: http://www.cplusplus.com/ref/cstdio/printf.html
here are some examples: x=.13241414515 x
0.13241414515 "%.4f" % x
'0.1324' "%.4g" % x
'0.1324'
x=5.13241414515 "%.4f" % x
'5.1324' "%.4g" % x
'5.132'
</F>
On 2005-11-09, Tuvas <tu*****@gmail.com> wrote: Wait, one more question. If the number is something like:
1.32042
It is like "1.32 stuff"
I would like it's size to remain constant. Any way around this? http://www.python.org/doc/current/li...s.html#l2h-211
--
Grant Edwards grante Yow! WHO sees a BEACH
at BUNNY sobbing on a SHAG
visi.com RUG?!
Grant Edwards wrote: On 2005-11-09, Tuvas <tu*****@gmail.com> wrote:
I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command.
Sorry, that's not possible.
Technically, it is. class Float4(float):
.... def __str__(self):
.... return '%.4g' % self
.... x = Float4(1.23456789) str(x)
'1.235'
But, of course, I'd recommend just using '%.4g'%x directly.
On 2005-11-10, Dan Bishop <da*****@yahoo.com> wrote: Grant Edwards wrote: On 2005-11-09, Tuvas <tu*****@gmail.com> wrote:
> I would like to limit a floating variable to 4 signifigant digits, when > running thorugh a str command.
Sorry, that's not possible.
Technically, it is.
Ah well, I assumed that by "floating variable" he meant the
built-in "float" type.
--
Grant Edwards grante Yow! I always wanted a
at NOSE JOB!!
visi.com
Tuvas wrote: Wait, one more question. If the number is something like:
1.32042
It is like "1.32 stuff"
I would like it's size to remain constant. Any way around this?
s/%g/%f print "%.4f stuff" % 1.3241414515
1.3241 stuff print "%.4f stuff" % 1.32042
1.3204 stuff
"Tuvas" <tu*****@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com... I would like to limit a floating variable to 4 signifigant digits, when running thorugh a str command. Ei,
x=.13241414515 y=str(x)+" something here"
But somehow limiting that to 4 sign. digits. I know that if you use the print statement, you can do something like %.4d, but how can I do this with converting the number to a string? Thanks!
from scipy import round
print round(0.13241414515,4)
Regards,
Christian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roger Leigh |
last post by:
Hello,
I'm writing a fixed-precision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a...
|
by: Dave |
last post by:
Hi folks,
I am trying to develop a routine that will handle sphere-sphere and
sphere-triangle collisions and interactions. My aim is to develop a
quake style collision engine where a player can...
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't...
|
by: Shawn |
last post by:
Hello all,
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to...
|
by: Mark L Pappin |
last post by:
<puts on Compiler Vendor hat>
I've recently discovered that our compilers don't
make any attempt to handle floating point overflow in add/subtract/
multiply/divide, with the result that...
| |
by: Vinoth |
last post by:
I'm working in an ARM (ARM9) system which does not have Floating point
co-processor or Floating point libraries. But it does support long long int
(64 bits).
Can you provide some link that would...
|
by: michael.mcgarry |
last post by:
Hi,
I have a question about floating point precision in C.
What is the minimum distinguishable difference between 2 floating point
numbers? Does this differ for various computers?
Is this...
|
by: ma740988 |
last post by:
template <class T> inline bool isEqual( const T& a, const T& b,
const T epsilon = std::numeric_limits<T>::epsilon() )
{
const T diff = a - b;
return ( diff <= epsilon ) && ( diff >= -epsilon );...
|
by: Peter |
last post by:
I have written this small app to explain an issue I'm having with a larger
program.
In the following code I'm taking 10 ints from the keyboard. In the call to
average() these 10 ints are then...
|
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,...
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
| |