1 //! Number format enumerations and bit masks. 2 3 // Sample test code for each language used: 4 // 5 // Rust 6 // ---- 7 // 8 // Setup: 9 // Save to `main.rs` and run `rustc main.rs -o main`. 10 // 11 // Code: 12 // ```text 13 // pub fn main() { 14 // println!("{:?}", 3_.0f32); 15 // println!("{:?}", "3_.0".parse::<f32>()); 16 // } 17 // ``` 18 // 19 // Python 20 // ------ 21 // 22 // Setup: 23 // Run `python` to enter the interpreter. 24 // 25 // Code: 26 // ```text 27 // print(3_.0) 28 // print(float("3_.0")) 29 // ``` 30 // 31 // C++ 32 // --- 33 // 34 // Setup: 35 // Save to `main.cc` and run `g++ main.cc -o main -std=c++XX`, 36 // where XX is one of the following values: 37 // - 98 38 // - 03 39 // - 11 40 // - 14 41 // - 17 42 // 43 // Code: 44 // ```text 45 // #include <cstdlib> 46 // #include <cstring> 47 // #include <iostream> 48 // #include <iterator> 49 // #include <stdexcept> 50 // 51 // double parse(const char* string) { 52 // char* end; 53 // double result = strtod(string, &end); 54 // if (std::distance(string, reinterpret_cast<const char*>(end)) != strlen(string)) { 55 // throw std::invalid_argument("did not consume entire string."); 56 // } 57 // return result; 58 // } 59 // 60 // int main() { 61 // std::cout << 3'.0 << std::endl; 62 // std::cout << parse("3'.0") << std::endl; 63 // } 64 // ``` 65 // 66 // C 67 // - 68 // 69 // Setup: 70 // Save to `main.c` and run `gcc main.c -o main -std=cXX`, 71 // where XX is one of the following values: 72 // - 89 73 // - 90 74 // - 99 75 // - 11 76 // - 18 77 // 78 // Code: 79 // ```text 80 // #include <stdint.h> 81 // #include <stdlib.h> 82 // #include <string.h> 83 // #include <stdio.h> 84 // 85 // size_t distance(const char* first, const char* last) { 86 // uintptr_t x = (uintptr_t) first; 87 // uintptr_t y = (uintptr_t) last; 88 // return (size_t) (y - x); 89 // } 90 // 91 // double parse(const char* string) { 92 // char* end; 93 // double result = strtod(string, &end); 94 // if (distance(string, (const char*) end) != strlen(string)) { 95 // abort(); 96 // } 97 // return result; 98 // } 99 // 100 // int main() { 101 // printf("%f\n", 3'.); 102 // printf("%f\n", parse("3'.")); 103 // } 104 // ``` 105 // 106 // Ruby 107 // ---- 108 // 109 // Setup: 110 // Run `irb` to enter the interpreter. 111 // 112 // Code: 113 // ```text 114 // puts 3.0_1; 115 // puts "3.0_1".to_f; 116 // ``` 117 // Swift 118 // ----- 119 // 120 // Setup: 121 // Run `swift` to enter the interpreter. 122 // 123 // Code: 124 // ```text 125 // print(3.0); 126 // print(Float("3.0")); 127 // ``` 128 // Golang 129 // ------ 130 // 131 // Setup: 132 // Save to `main.go` and run `go run main.go` 133 // 134 // Code: 135 // ```text 136 // package main 137 // 138 // import ( 139 // "fmt" 140 // "strconv" 141 // ) 142 // 143 // func main() { 144 // fmt.Println(3.0) 145 // fmt.Println(strconv.ParseFloat("3.0", 64)) 146 // } 147 // ``` 148 // 149 // Haskell 150 // ------- 151 // 152 // Setup: 153 // Run `ghci` to enter the interpreter. 154 // 155 // Code: 156 // ```text 157 // :m Numeric 158 // showFloat 3.0 "" 159 // let x = "3.0" 160 // read x :: Float 161 // ``` 162 // 163 // Javascript 164 // ---------- 165 // 166 // Setup: 167 // Run `nodejs` (or `node`) to enter the interpreter. 168 // 169 // Code: 170 // ```text 171 // console.log(3.0) 172 // console.log(parseFloat("3.0")) 173 // ``` 174 // 175 // Perl 176 // ---- 177 // 178 // Setup: 179 // Run `perl -de1` to enter the interpret. 180 // 181 // Code: 182 // ```text 183 // print 3.01; 184 // print '3.01' * 1; 185 // ``` 186 // 187 // PHP 188 // --- 189 // 190 // Setup: 191 // Run `php -a` to enter the interpret. 192 // 193 // Code: 194 // ```text 195 // printf("%f\n", 3.0); 196 // printf("%f\n", floatval("3.0")); 197 // ``` 198 // 199 // Java 200 // ---- 201 // 202 // Setup: 203 // Save to `main.java` and run `javac main.java`, then run `java Main`. 204 // 205 // Code: 206 // ```text 207 // class Main { 208 // public static void main(String args[]) { 209 // System.out.println(3.0); 210 // System.out.println(Float.parseFloat("3.0")); 211 // } 212 // } 213 // ``` 214 // 215 // R 216 // - 217 // 218 // Setup: 219 // Run `R` to enter the interpret. 220 // 221 // Code: 222 // ```text 223 // print(3.0); 224 // print(as.numeric("3.0")); 225 // ``` 226 // 227 // Kotlin 228 // ------ 229 // 230 // Setup: 231 // Save file to `main.kt` and run `kotlinc main.kt -d main.jar`, 232 // then run `java -jar main.jar`. 233 // 234 // Code: 235 // ```text 236 // fun main() { 237 // println(3.0) 238 // println("3.0".toDouble()) 239 // } 240 // ``` 241 // 242 // Julia 243 // ----- 244 // 245 // Setup: 246 // Run `julia` to enter the interpret. 247 // 248 // Code: 249 // ```text 250 // print(3.0); 251 // print(parse(Float64, "3.0")); 252 // ``` 253 // 254 // C# 255 // -- 256 // 257 // Note: 258 // Mono accepts both integer and fraction decimal separators, Mono is 259 // just buggy, see https://github.com/dotnet/csharplang/issues/55#issuecomment-574902516. 260 // 261 // Setup: 262 // Run `csharp -langversion:X` to enter the interpret, 263 // where XX is one of the following values: 264 // - ISO-1 265 // - ISO-2 266 // - 3 267 // - 4 268 // - 5 269 // - 6 270 // - 7 271 // 272 // Code: 273 // ```text 274 // Console.WriteLine("{0}", 3.0); 275 // Console.WriteLine("{0}", float.Parse("3.0")); 276 // ``` 277 // 278 // Kawa 279 // ---- 280 // 281 // Setup: 282 // Run `kawa` to enter the interpreter. 283 // 284 // Code: 285 // ```text 286 // 3.0 287 // (string->number "3.0") 288 // ``` 289 // 290 // Gambit-C 291 // -------- 292 // 293 // Setup: 294 // Run `gsc` to enter the interpreter. 295 // 296 // Code: 297 // ```text 298 // 3.0 299 // (string->number "3.0") 300 // ``` 301 // 302 // Guile 303 // ----- 304 // 305 // Setup: 306 // Run `guile` to enter the interpreter. 307 // 308 // Code: 309 // ```text 310 // 3.0 311 // (string->number "3.0") 312 // ``` 313 // 314 // Clojure 315 // ------- 316 // 317 // Setup: 318 // Run `clojure` to enter the interpreter. 319 // 320 // Code: 321 // ```text 322 // 3.0 323 // (Float/parseFloat "3.0") 324 // ``` 325 // 326 // Erlang 327 // ------ 328 // 329 // Setup: 330 // Run `erl` to enter the interpreter. 331 // 332 // Code: 333 // ```text 334 // io:format("~p~n", [3.0]). 335 // string:to_float("3.0"). 336 // ``` 337 // 338 // Elm 339 // --- 340 // 341 // Setup: 342 // Run `elm repl` to enter the interpreter. 343 // 344 // Code: 345 // ```text 346 // 3.0 347 // String.toFloat "3.0" 348 // ``` 349 // 350 // Scala 351 // ----- 352 // 353 // Setup: 354 // Run `scala` to enter the interpreter. 355 // 356 // Code: 357 // ```text 358 // 3.0 359 // "3.0".toFloat 360 // ``` 361 // 362 // Elixir 363 // ------ 364 // 365 // Setup: 366 // Run `iex` to enter the interpreter. 367 // 368 // Code: 369 // ```text 370 // 3.0; 371 // String.to_float("3.0"); 372 // ``` 373 // 374 // FORTRAN 375 // ------- 376 // 377 // Setup: 378 // Save to `main.f90` and run `gfortran -o main main.f90` 379 // 380 // Code: 381 // ```text 382 // program main 383 // real :: x 384 // character (len=30) :: word 385 // word = "3." 386 // read(word, *) x 387 // print *, 3. 388 // print *, x 389 // end program main 390 // ``` 391 // 392 // D 393 // - 394 // 395 // Setup: 396 // Save to `main.d` and run `dmd -run main.d` 397 // 398 // Code: 399 // ```text 400 // import std.conv; 401 // import std.stdio; 402 // 403 // void main() 404 // { 405 // writeln(3.0); 406 // writeln(to!double("3.0")); 407 // } 408 // ``` 409 // 410 // Coffeescript 411 // ------------ 412 // 413 // Setup: 414 // Run `coffee` to enter the interpreter. 415 // 416 // Code: 417 // ```text 418 // 3.0; 419 // parseFloat("3.0"); 420 // ``` 421 // 422 // Cobol 423 // ----- 424 // 425 // Setup: 426 // Save to `main.cbl` and run `cobc main.cbl` then `cobcrun main`. 427 // 428 // Code: 429 // ```text 430 // IDENTIFICATION DIVISION. 431 // PROGRAM-ID. main. 432 // 433 // DATA DIVISION. 434 // WORKING-STORAGE SECTION. 435 // 01 R PIC X(20) VALUE "3.0". 436 // 01 TOTAL USAGE IS COMP-2. 437 // 438 // PROCEDURE DIVISION. 439 // COMPUTE TOTAL = FUNCTION NUMVAL(R). 440 // Display 3.0. 441 // Display TOTAL. 442 // STOP RUN. 443 // ``` 444 // 445 // F# 446 // -- 447 // 448 // Setup: 449 // Run `fsharpi` to enter the interpreter. 450 // 451 // Code: 452 // ```text 453 // printfn "%f" 3.0;; 454 // let f = float "3.0";; 455 // printfn "%f" f;; 456 // ``` 457 // 458 // Visual Basic 459 // ------------ 460 // 461 // Setup: 462 // Save to `main.vb` and run `vbnc main.vb`. 463 // 464 // Code: 465 // ```text 466 // Imports System 467 // 468 // Module Module1 469 // Sub Main() 470 // Console.WriteLine(Format$(3.0, "0.0000000000000")) 471 // Console.WriteLine(Format$(CDbl("3.0"), "0.0000000000000")) 472 // End Sub 473 // End Module 474 // ``` 475 // 476 // OCaml 477 // ----- 478 // 479 // Setup: 480 // Save to `main.ml` and run `ocamlc -o main main.ml`. 481 // 482 // Code: 483 // ```text 484 // Printf.printf "%f\n" 3.0 485 // let () = 486 // let f = float_of_string "3.0" in 487 // Printf.printf "%f\n" f 488 // ``` 489 // 490 // Objective-C 491 // ----------- 492 // 493 // Setup: 494 // Save to `main.m` and run `gcc -o main -lobjc -lgnustep-base main.m -fconstant-string-class=NSConstantString`. 495 // 496 // Code: 497 // ```text 498 // #import <Foundation/Foundation.h> 499 // #import <stdio.h> 500 // 501 // int main(int argv, char* argc[]) 502 // { 503 // printf("%f\n", 3.0); 504 // NSString *s = @"3.0"; 505 // double f = [s doubleValue]; 506 // printf("%f\n", f); 507 // } 508 // ``` 509 // 510 // ReasonML 511 // -------- 512 // 513 // Setup: 514 // Run `rtop` to enter the interpreter. 515 // 516 // Code: 517 // ```text 518 // Printf.printf("%f\n", 3.0); 519 // Printf.printf("%f\n", float_of_string("3.0")); 520 // ``` 521 // 522 // Zig 523 // --- 524 // 525 // Setup: 526 // Save to `main.zig` and run `zig build-exe main.zig` 527 // 528 // Code: 529 // ```text 530 // const std = @import("std"); 531 // 532 // pub fn main() void { 533 // const f: f64 = 3.0; 534 // std.debug.warn("{}\n", f); 535 // const x: f64 = std.fmt.parseFloat(f64, "3.0") catch unreachable; 536 // std.debug.warn("{}\n", x); 537 // } 538 // ``` 539 // 540 // 541 // Octave (and Matlab) 542 // ------------------- 543 // 544 // Setup: 545 // Run `octave` to enter the interpreter, or 546 // run `octave --traditional` to enter the Matlab interpret. 547 // 548 // Code: 549 // ```text 550 // 3.0 551 // str2double("3.0") 552 // ``` 553 // 554 // Sage 555 // ---- 556 // 557 // Setup: 558 // Run `sage` to enter the interpreter. 559 // 560 // Code: 561 // ```text 562 // 3.0 563 // float("3.0") 564 // ``` 565 // 566 // JSON 567 // ---- 568 // 569 // Setup: 570 // Run `node` (or `nodejs`) to enter the JS interpreter. 571 // 572 // Code: 573 // ```text 574 // JSON.parse("3.0") 575 // ``` 576 // 577 // TOML 578 // ---- 579 // 580 // Setup: 581 // Run `python` to enter the Python interpreter. 582 // 583 // Code: 584 // ```text 585 // import tomlkit 586 // tomlkit.parse("a = 3.0") 587 // ``` 588 // 589 // XML 590 // --- 591 // 592 // Setup: 593 // Run `python` to enter the Python interpreter. 594 // 595 // Code: 596 // ```text 597 // from lxml import etree 598 // 599 // def validate_xml(xsd, xml): 600 // '''Validate XML file against schema''' 601 // 602 // schema = etree.fromstring(xsd) 603 // doc = etree.fromstring(xml) 604 // xmlschema = etree.XMLSchema(schema) 605 // 606 // return xmlschema.validate(doc) 607 // 608 // 609 // xsd = b'''<?xml version="1.0" encoding="UTF-8"?> 610 // <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 611 // <xs:element name="prize" type="xs:float"/> 612 // </xs:schema>''' 613 // 614 // xml = b'''<?xml version="1.0" encoding="UTF-8"?> 615 // <prize>3.0</prize> 616 // ''' 617 // 618 // validate_xml(xsd, xml) 619 // ``` 620 // 621 // SQLite 622 // ------ 623 // 624 // Setup: 625 // Run `sqlite3 :memory:` to enter the sqlite3 interpreter 626 // with an in-memory database. 627 // 628 // Code: 629 // ```text 630 // CREATE TABLE stocks (price real); 631 // INSERT INTO stocks VALUES (3.0); 632 // SELECT * FROM stocks; 633 // ``` 634 // 635 // PostgreSQL 636 // ---------- 637 // 638 // Setup: 639 // Run `initdb -D db` to create a database data direction, 640 // then run `pg_ctl -D db start` to start the server, then run 641 // `createdb` to create a user database and `psql` to start the 642 // interpreter. 643 // 644 // Code: 645 // ```text 646 // CREATE TABLE stocks (price real); 647 // INSERT INTO stocks VALUES (3.0); 648 // SELECT * FROM stocks; 649 // ``` 650 // 651 // MySQL 652 // ----- 653 // 654 // Setup: 655 // Run `mysqld` to start the server, then run `mysql` to start the 656 // interpreter. 657 // 658 // Code: 659 // ```text 660 // USE mysql; 661 // CREATE TABLE stocks (price real); 662 // INSERT INTO stocks VALUES (3.0); 663 // SELECT * FROM stocks; 664 // ``` 665 // 666 // MongoDB 667 // ------- 668 // 669 // Setup: 670 // Run `mongod --dbpath data/db` to start the server, then run 671 // `mongo` to start the interpreter. 672 // 673 // Code: 674 // ```text 675 // use mydb 676 // db.movie.insert({"name": 3.0}) 677 // db.movie.find() 678 // ``` 679 680 cfg_if! { 681 if #[cfg(feature = "format")] { 682 mod feature_format; 683 pub use self::feature_format::*; 684 } else { 685 mod not_feature_format; 686 pub use self::not_feature_format::*; 687 } 688 } 689