473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating numbers and str

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!

Nov 9 '05 #1
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

Nov 9 '05 #2
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!


Nov 9 '05 #3
Yep, I was thinking in C, not python. Thanks for the help!

Nov 9 '05 #4
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!!
Nov 9 '05 #5
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?

Nov 9 '05 #6
"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>

Nov 9 '05 #7
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?!
Nov 9 '05 #8
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.

Nov 10 '05 #9
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
Nov 10 '05 #10
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

Nov 10 '05 #11
"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

Nov 10 '05 #12

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

Similar topics

4
7824
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...
4
3285
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...
687
22801
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...
10
2232
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...
3
3760
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...
7
3369
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...
15
3892
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...
32
4045
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 );...
11
2043
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...
0
7126
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,...
0
7005
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...
0
7168
Oralloy
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,...
1
6891
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...
0
7381
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...
1
4916
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...
0
4595
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...
0
3096
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...
0
293
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...

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.