Need to format numbers with scientific and engineeringformats.

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Need to format numbers with scientific and engineeringformats.

    Hi,

    Does anybody have a recipe for forcing scientific notation? Maybe something like C's sprintf function ...
    EG: "sprintf (buffer, "%5.3E", ...);"

    Thanks,
    -- MikeR

    MikerRoo Guest

  2. Similar Questions and Discussions

    1. scientific calculation in flash?
      can I implement some scientific calculation in flash? I mean , can I write a public method, e.g. calculate(dataId), and call it from JavaScript and...
    2. Howto cite postgresql in scientific publications?
      Hi, Is there an official way to cite PostgreSQL in scientific publications? This might sound somewhat strange to some of you, but e.g. the R...
    3. FREE! 1000+ Scientific Symbols for Illustrator
      Very good info...thanks. On a related topic, does anybody know a good site to get "landscape design" symbols? I'm talking about the symbols...
    4. format numbers with parentheses
      Hi all, I want to format right-aligned positive and negative numbers in a grid. My problem is that I want the negs in parentheses, which is...
    5. Numerical/Scientific programming ruby group
      Hi Gordon, Gordon James Miller wrote: I'd be vaguely interested in this, because I've been doing a lot of numerical stuff at uni lately. I...
  3. #2

    Default Re: Need to format numbers with scientific andengineering formats.

    maybe overkill for you but i've used java.text.DecimalFormat for that in i18n apps.
    PaulH Guest

  4. #3

    Default Re: Need to format numbers with scientific andengineering formats.

    Thanks, Paul.

    The JAVA version of DecimalFormat() looks like it would be an improvement.
    But I can't get it to work.

    For example when trying to use one of the format functions, like:
    <CFSET zJ_Txt = CreateObject("Java","java.text.DecimalFormat")>
    <h3>Attempt2 to format = #zJ_Txt2.format (JavaCast ("double",
    123.4567890001))#.</h3>

    I get:
    " The selected method format was not found.
    Either there are no methods with the specified method name and argument types,
    or the method format is overloaded with arguments types that ColdFusion can't
    decipher reliably. If this is a Java object and you verified that the method
    exists, you may need to use the javacast function to reduce ambiguity."

    I only seem to be able to use the non-overloaded functions, like toPattern().

    Regards,
    -- MikeR



    MikerRoo Guest

  5. #4

    Default Re: Need to format numbers with scientific andengineering formats.

    sorry it's been so long that i had to struggle w/the internals of this sort of
    thing (it's all buried in a CFC) i forgot you have to write wrappers for all
    the numerical format() methods. see the code below (i ripped out the i18n bits
    but haven't tested it,so double check). compile it and toss the .class file
    into your cf_Install\wwwroot\WEB-INF\classes dir.


    import java.text.DecimalFormat;
    import java.text.NumberFormat;

    public class scientificFormat {
    public final static String scientificFormatter(Double thisNumber, String
    thisPattern) {
    NumberFormat dF = new DecimalFormat(thisPattern);
    String scientificStr= dF.format(thisNumber);
    return scientificStr;
    }
    }

    PaulH Guest

  6. #5

    Default Re: Need to format numbers with scientific andengineering formats.

    Thanks, Paul.

    It worked.

    -- MikeR

    MikerRoo Guest

  7. #6

    Default Re: Need to format numbers with scientific andengineering formats.

    btw that's going to return formatting based on your server's default locale, so
    look at that if it starts throwing crazy stuff back at you if you move the app.
    and while i'm thinking about this if you need more ganular control over
    rounding, padding, and significant digits you might want to look at icu4j's
    DecimalFormat class intead of the core java class (that's actually what we used
    but we use that IBM lib for all our i18n stuff).

    PaulH Guest

  8. #7

    Default Re: Need to format numbers with scientific andengineering formats.

    Thanks again, Paul.

    I had figured out the locale stuff when I looked up DecimalFormat. That and
    the instruction to compile your own java class was the key advice. (I had no
    clue what java's formatting functions were (We bought CF so we wouldn't have to
    use java :P )).

    DecimalFormat () is actually perfect for now (with a few custom post-string
    tweaks).

    For future reference, I've since discovered that Sun replicates the full
    sprintf functionality with its PrintfFormat class and Henrik Bengtsson has
    replicated the sprintf family nicely in java.

    Regards,
    -- MikeR


    MikerRoo Guest

  9. #8

    Default Re: Need to format numbers with scientific andengineering formats.

    i'm not familiar w/PrintfFormat (and i don't see it in the API for 1.4 or 1.5).
    what package is that in?

    just in case you haven't thought about this already, users have the pesky
    habit of wanting you to
    parse those fancily formatted numbers your app produces back into something
    suitable for a db, etc. which is nearly impossible in many cases if your
    formattng strays very far from the "standards".

    PaulH Guest

  10. #9

    Default Re: Need to format numbers with scientific andengineering formats.

    PrintfFormat is not in a package. It's an add-on class that Sun provides to
    appease C programmers.
    More information can be found at
    [url]http://java.sun.com/developer/technicalArticles/Programming/sprintf/[/url]

    I know all about having to endlessly tweak report and number formats.
    However, my shop is lucky in that we don't have to do any internationalization.

    In fact, our guy, Calvin, says that when he's in charge, there will be only
    one locale and everyone will speak esperanto.


    MikerRoo Guest

  11. #10

    Default Re: Need to format numbers with scientific andengineering formats.

    well we don't endlessly tweak formats. if the data has to be two-way we (via
    our i18n CFC) only allow formatting using 1 of the 4 standard styles (which
    parses easily enough), if its really customized its only one way ;-)

    esperanto? well it is recognized as a genuine language by the CLDR project but
    i don't see any locales in core java or icu4j (via getAvailableLocales) for it.
    i guess your friend has a tough row to hoe.

    and thanks for the link, i'll have a closer look at it. did java 1.5 add
    anything for this? i seem to recall something about formatting messages or
    something along those lines.







    PaulH Guest

  12. #11

    Default Re: Need to format numbers with scientific andengineering formats.

    Don't know if java 1.5 made any improvements in this area. I do know that
    classes compiled with the 1.5 SDK are incompatible with CF 7 (except that I
    didn't try running enterprise on a 1.5 App server -- since our production
    server, in this case, is running the CF standard edition).

    Trying to use 1.5 or SDK 5 classes resulted in JRUN "major/minor version"
    errors. We reverted to SDK 1.4.2 and left it at that.

    Anyway this fire's out. Thanks for your help.


    MikerRoo Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139