@@ -18,6 +18,9 @@ use cmath;
18
18
use cmp;
19
19
use libc:: { c_float, c_int} ;
20
20
use num;
21
+ use option:: Option ;
22
+ use from_str;
23
+ use to_str;
21
24
22
25
pub use cmath:: c_float_targ_consts:: * ;
23
26
@@ -333,6 +336,197 @@ impl f32: num::Round {
333
336
}
334
337
}
335
338
339
+ /**
340
+ * Section: String Conversions
341
+ */
342
+
343
+ /**
344
+ * Converts a float to a string
345
+ *
346
+ * # Arguments
347
+ *
348
+ * * num - The float value
349
+ */
350
+ #[ inline( always) ]
351
+ pub pure fn to_str ( num : f32 ) -> ~str {
352
+ let ( r, _) = num:: to_str_common (
353
+ & num, 10 u, true , true , num:: SignNeg , num:: DigAll ) ;
354
+ r
355
+ }
356
+
357
+ /**
358
+ * Converts a float to a string in hexadecimal format
359
+ *
360
+ * # Arguments
361
+ *
362
+ * * num - The float value
363
+ */
364
+ #[ inline( always) ]
365
+ pub pure fn to_str_hex ( num : f32 ) -> ~str {
366
+ let ( r, _) = num:: to_str_common (
367
+ & num, 16 u, true , true , num:: SignNeg , num:: DigAll ) ;
368
+ r
369
+ }
370
+
371
+ /**
372
+ * Converts a float to a string in a given radix
373
+ *
374
+ * # Arguments
375
+ *
376
+ * * num - The float value
377
+ * * radix - The base to use
378
+ */
379
+ #[ inline( always) ]
380
+ pub pure fn to_str_radix ( num : f32 , rdx : uint ) -> ~str {
381
+ let ( r, _) = num:: to_str_common (
382
+ & num, rdx, true , true , num:: SignNeg , num:: DigAll ) ;
383
+ r
384
+ }
385
+
386
+ /**
387
+ * Converts a float to a string with exactly the number of
388
+ * provided significant digits
389
+ *
390
+ * # Arguments
391
+ *
392
+ * * num - The float value
393
+ * * digits - The number of significant digits
394
+ */
395
+ #[ inline( always) ]
396
+ pub pure fn to_str_exact ( num : f32 , dig : uint ) -> ~str {
397
+ let ( r, _) = num:: to_str_common (
398
+ & num, 10 u, true , true , num:: SignNeg , num:: DigExact ( dig) ) ;
399
+ r
400
+ }
401
+
402
+ /**
403
+ * Converts a float to a string with a maximum number of
404
+ * significant digits
405
+ *
406
+ * # Arguments
407
+ *
408
+ * * num - The float value
409
+ * * digits - The number of significant digits
410
+ */
411
+ #[ inline( always) ]
412
+ pub pure fn to_str_digits ( num : f32 , dig : uint ) -> ~str {
413
+ let ( r, _) = num:: to_str_common (
414
+ & num, 10 u, true , true , num:: SignNeg , num:: DigMax ( dig) ) ;
415
+ r
416
+ }
417
+
418
+ impl f32 : to_str:: ToStr {
419
+ #[ inline( always) ]
420
+ pure fn to_str ( ) -> ~str { to_str_digits ( self , 8 ) }
421
+ }
422
+
423
+ impl f32 : num:: ToStrRadix {
424
+ #[ inline( always) ]
425
+ pure fn to_str_radix ( & self , rdx : uint ) -> ~str {
426
+ to_str_radix ( * self , rdx)
427
+ }
428
+ }
429
+
430
+ /**
431
+ * Convert a string in base 10 to a float.
432
+ * Accepts a optional decimal exponent.
433
+ *
434
+ * This function accepts strings such as
435
+ *
436
+ * * '3.14'
437
+ * * '+3.14', equivalent to '3.14'
438
+ * * '-3.14'
439
+ * * '2.5E10', or equivalently, '2.5e10'
440
+ * * '2.5E-10'
441
+ * * '.' (understood as 0)
442
+ * * '5.'
443
+ * * '.5', or, equivalently, '0.5'
444
+ * * '+inf', 'inf', '-inf', 'NaN'
445
+ *
446
+ * Leading and trailing whitespace represent an error.
447
+ *
448
+ * # Arguments
449
+ *
450
+ * * num - A string
451
+ *
452
+ * # Return value
453
+ *
454
+ * `none` if the string did not represent a valid number. Otherwise,
455
+ * `Some(n)` where `n` is the floating-point number represented by `num`.
456
+ */
457
+ #[ inline( always) ]
458
+ pub pure fn from_str ( num : & str ) -> Option < f32 > {
459
+ num:: from_str_common ( num, 10 u, true , true , true , num:: ExpDec , false )
460
+ }
461
+
462
+ /**
463
+ * Convert a string in base 16 to a float.
464
+ * Accepts a optional binary exponent.
465
+ *
466
+ * This function accepts strings such as
467
+ *
468
+ * * 'a4.fe'
469
+ * * '+a4.fe', equivalent to 'a4.fe'
470
+ * * '-a4.fe'
471
+ * * '2b.aP128', or equivalently, '2b.ap128'
472
+ * * '2b.aP-128'
473
+ * * '.' (understood as 0)
474
+ * * 'c.'
475
+ * * '.c', or, equivalently, '0.c'
476
+ * * '+inf', 'inf', '-inf', 'NaN'
477
+ *
478
+ * Leading and trailing whitespace represent an error.
479
+ *
480
+ * # Arguments
481
+ *
482
+ * * num - A string
483
+ *
484
+ * # Return value
485
+ *
486
+ * `none` if the string did not represent a valid number. Otherwise,
487
+ * `Some(n)` where `n` is the floating-point number represented by `[num]`.
488
+ */
489
+ #[ inline( always) ]
490
+ pub pure fn from_str_hex ( num : & str ) -> Option < f32 > {
491
+ num:: from_str_common ( num, 16 u, true , true , true , num:: ExpBin , false )
492
+ }
493
+
494
+ /**
495
+ * Convert a string in an given base to a float.
496
+ *
497
+ * Due to possible conflicts, this function does **not** accept
498
+ * the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
499
+ * does it recognize exponents of any kind.
500
+ *
501
+ * Leading and trailing whitespace represent an error.
502
+ *
503
+ * # Arguments
504
+ *
505
+ * * num - A string
506
+ * * radix - The base to use. Must lie in the range [2 .. 36]
507
+ *
508
+ * # Return value
509
+ *
510
+ * `none` if the string did not represent a valid number. Otherwise,
511
+ * `Some(n)` where `n` is the floating-point number represented by `num`.
512
+ */
513
+ #[ inline( always) ]
514
+ pub pure fn from_str_radix ( num : & str , rdx : uint ) -> Option < f32 > {
515
+ num:: from_str_common ( num, rdx, true , true , false , num:: ExpNone , false )
516
+ }
517
+
518
+ impl f32 : from_str:: FromStr {
519
+ #[ inline( always) ]
520
+ static pure fn from_str( val: & str ) -> Option < f32 > { from_str ( val ) }
521
+ }
522
+
523
+ impl f32 : num:: FromStrRadix {
524
+ #[ inline ( always ) ]
525
+ static pure fn from_str_radix ( val : & str , rdx : uint ) -> Option < f32 > {
526
+ from_str_radix ( val , rdx )
527
+ }
528
+ }
529
+
336
530
//
337
531
// Local Variables:
338
532
// mode: rust
0 commit comments