Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Tuesday, September 28, 2021

Flutter – How to limit keyboard to allow digits only - How to Make TextField Number only in Flutter - Digits Only Input Keyboard in Flutter – Only Numbers on Text Field - How to Create Number Inputfield in Flutter

This post is just my quick note on how to limit the soft keyboard to allow only digits.

TextField is one of the most commonly used widgets in Flutter. It helps users to enter their text input. In this tutorial, let’s learn how to make the textfield number only by changing the keyboard type in Flutter and apply regex to restrict other than valid numbers.

Kyebord type input only show a keyboard with number inputs, but you can add like 4....4 which is not a number anyway, so let apply regex too to avoid such type of situation.
You can specify the number as keyboard type for the TextField Widget using:
keyboardType: TextInputType.number

Through this option, you can strictly restrict another char without a number.
inputFormatters: [FilteringTextInputFormatter.digitsOnly], keyboardType: TextInputType.number,
Lets apply regex to TextField to avoid unexpected input to TextField:

I'm going to full example, first, created a file named input_validation_utils.dart with following contents:
import 'package:flutter/services.dart';

abstract class StringValidator {
  bool isValid(String value);
}

class DecimalNumberEditValidator extends RegexValidator {
  DecimalNumberEditValidator() : super(regexSource: "^\$|^(0|([1-9][0-9]{0,7}))(\\.[0-9]{0,2})?\$");
}

class RegexValidator implements StringValidator {
  RegexValidator({required this.regexSource});
  final String regexSource;

  /// value: the input string
  /// returns: true if the input string is a full match for regexSource
  bool isValid(String value) {
    try {
      final regex = RegExp(regexSource);
      final matches = regex.allMatches(value);
      for (Match match in matches) {
        if (match.start == 0 && match.end == value.length) {
          return true;
        }
      }
      return false;
    }
    catch (e) {
      // Invalid regex
      assert(false, e.toString());
      return true;
    }
  }
}

class ValidatorInputFormatter implements TextInputFormatter {
  ValidatorInputFormatter({required this.editingValidator});
  final StringValidator editingValidator;

  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    final oldValueValid = editingValidator.isValid(oldValue.text);
    final newValueValid = editingValidator.isValid(newValue.text);
    if (oldValueValid && !newValueValid) {
      return oldValue;
    }
    return newValue;
  }
}
Then we need to apply above validator to a TextField to restrict input other than numbers, now main.dart is looks like:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:p1/input_validation_utils.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title = "Regex Input Validation";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ListView(
        children: <Widget>[
          SizedBox(height: 30,),
          TextField(
            decoration: InputDecoration.collapsed(hintText: '\$0.00'),
            style: TextStyle(fontSize: 32.0, color: Colors.black87),
            textAlign: TextAlign.center,
            keyboardType: TextInputType.number,
            autofocus: true,
            //disabled auto correct if regex failed
            autocorrect: false,
            textInputAction: TextInputAction.done,
            inputFormatters: [
              //applying our custom formatter to TextField
              //so that input other than number can be input
              ValidatorInputFormatter(
                editingValidator: DecimalNumberEditValidator(),
              )
            ],
            onChanged: (value) {
              print("Input=${value}");
            },
          ),
          SizedBox(height: 30,),
          OutlinedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => MyHomePage()),
                );
              },
              child: Text("Forward")
          )
        ],
      ),
    );
  }
}
Sample screen shot as below:

Saturday, May 25, 2019

How to deal with big numbers in javascript | Extremely large numbers in javascript | Large Number Addition in JavaScript | How to deal with extremly big numbers in javascript |

Javascript supports at most 53 bit integers. What this means is that integers larger than 53 bits lose precision. This article will explain how to handle addition with integers larger than 53 bits in javascript.
I'm looking for a Mathematical solution that deals with really (long, big, huge, storms) numbers. I haven't found anything yet, But I don't wanna think that this problem hasn't be solve at this time. I'm looking for an easy Number solution, like Microsoft Excel Precision (30 decimals), or a BigInteger (Java) solution. in Javascript of course.
JavaScript is only capable of handling 53-bit numbers, if you are working with a big number such as Twitter ID, which is using 64-bit number, then you need to find an external library to do that, otherwise, there will be precision lost.
View in JSFiddle
<script type="text/javascript" src="big-number.js"></script>

<script type="text/javascript">
    (function () {
        var a = new BigNumber("07777777"), b = "888888880";
        console.log(a.multiply(b).toString());
        console.log(new BigNumber("990").toString());
    })();
</script>
Download Library

Friday, June 6, 2014

Java Formatting Price/Number According To Country/Currency

External Link


CurrencyFormat.java


import java.util.*;
import java.lang.*;
import java.text.*;


class CurrencyFormat {
 public static void main (String[] args) throws java.lang.Exception {
 Map<String, String> languagesMap = new TreeMap<String, String>();
        Locale[] locales = Locale.getAvailableLocales();
        for (Locale obj : locales) {
            if ((obj.getDisplayCountry() != null) && (!"".equals(obj.getDisplayCountry()))) {
                languagesMap.put(obj.getCountry(), obj.getLanguage());
            }
        }

        String[] countries = Locale.getISOCountries();
        int supportedLocale = 0, nonSupportedLocale = 0;
        for (String countryCode : countries) {
            Locale obj = null;
            if (languagesMap.get(countryCode) == null) {
                obj = new Locale("", countryCode);
                nonSupportedLocale++;
            }
            else {
                //create a Locale with own country's languages
                obj = new Locale(languagesMap.get(countryCode), countryCode);
                supportedLocale++;

            }
            NumberFormat currency = NumberFormat.getCurrencyInstance(obj);
            DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) currency).getDecimalFormatSymbols();
            String currencySymbol = decimalFormatSymbols.getCurrencySymbol();
            decimalFormatSymbols.setCurrencySymbol("");
            ((DecimalFormat) currency).setDecimalFormatSymbols(decimalFormatSymbols);

            System.out.println("Country Code = " + obj.getISO3Country()
                    + ", Price = " + currency.format(35147869.3518).trim()
                    + ", Symbol: " + currencySymbol
                    + ", Name: " + decimalFormatSymbols.getCurrency().getDisplayName()
                    + ", Code: " + decimalFormatSymbols.getCurrency().getCurrencyCode()
                    + ", Country Name = " + obj.getDisplayCountry(obj)
                    + ", Country Name = " + obj.getDisplayCountry()
                    + ", Languages = " + obj.getDisplayLanguage());
        }
        System.out.println("nonSupportedLocale : " + nonSupportedLocale);
        System.out.println("supportedLocale : " + supportedLocale);
    }
}

Output


Country Code = AND, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ARE, Price = 35,147,869.35, Symbol: د.إ.‏, Code: AED
Country Code = AFG, Price = 35,147,869.35, Symbol: AFN, Code: AFN
Country Code = ATG, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = AIA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = ALB, Price = 35.147.869,35, Symbol: Lek, Code: ALL
Country Code = ARM, Price = 35,147,869.35, Symbol: AMD, Code: AMD
Country Code = ANT, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = AGO, Price = 35,147,869.35, Symbol: AOA, Code: AOA
Country Code = ATA, Price = 35,147,869.35, Symbol: ¤, Code: XXX
Country Code = ARG, Price = 35.147.869,35, Symbol: $, Code: ARS
Country Code = ASM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = AUT, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = AUS, Price = 35,147,869.35, Symbol: $, Code: AUD
Country Code = ABW, Price = 35,147,869.35, Symbol: AWG, Code: AWG
Country Code = ALA, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = AZE, Price = 35,147,869.35, Symbol: AZN, Code: AZN
Country Code = BIH, Price = 35.147.869,35, Symbol: КМ., Code: BAM
Country Code = BRB, Price = 35,147,869.35, Symbol: BBD, Code: BBD
Country Code = BGD, Price = 35,147,869.35, Symbol: BDT, Code: BDT
Country Code = BEL, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = BFA, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = BGR, Price = 35 147 869,35, Symbol: лв., Code: BGN
Country Code = BHR, Price = 35,147,869.352, Symbol: د.ب.‏, Code: BHD
Country Code = BDI, Price = 35,147,869, Symbol: BIF, Code: BIF
Country Code = BEN, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = BLM, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = BMU, Price = 35,147,869.35, Symbol: BMD, Code: BMD
Country Code = BRN, Price = 35,147,869.35, Symbol: BND, Code: BND
Country Code = BOL, Price = 35.147.869,35, Symbol: B$, Code: BOB
Country Code = BES, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = BRA, Price = 35.147.869,35, Symbol: R$, Code: BRL
Country Code = BHS, Price = 35,147,869.35, Symbol: BSD, Code: BSD
Country Code = BTN, Price = 35,147,869.35, Symbol: BTN, Code: BTN
Country Code = BVT, Price = 35,147,869.35, Symbol: NOK, Code: NOK
Country Code = BWA, Price = 35,147,869.35, Symbol: BWP, Code: BWP
Country Code = BLR, Price = 35 147 869, Symbol: Руб, Code: BYR
Country Code = BLZ, Price = 35,147,869.35, Symbol: BZD, Code: BZD
Country Code = CAN, Price = 35 147 869,35, Symbol: $, Code: CAD
Country Code = CCK, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = COD, Price = 35,147,869.35, Symbol: CDF, Code: CDF
Country Code = CAF, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = COG, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = CHE, Price = 35'147'869.35, Symbol: SFr., Code: CHF
Country Code = CIV, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = COK, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = CHL, Price = 35.147.869, Symbol: Ch$, Code: CLP
Country Code = CMR, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = CHN, Price = 35,147,869.35, Symbol: ¥, Code: CNY
Country Code = COL, Price = 35.147.869,35, Symbol: $, Code: COP
Country Code = CRI, Price = 35,147,869.35, Symbol: C, Code: CRC
Country Code = CUB, Price = 35,147,869.35, Symbol: CUP, Code: CUP
Country Code = CPV, Price = 35,147,869.35, Symbol: CVE, Code: CVE
Country Code = CUW, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = CXR, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = CYP, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = CZE, Price = 35 147 869,35, Symbol: Kč, Code: CZK
Country Code = DEU, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = DJI, Price = 35,147,869, Symbol: DJF, Code: DJF
Country Code = DNK, Price = 35.147.869,35, Symbol: kr, Code: DKK
Country Code = DMA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = DOM, Price = 35,147,869.35, Symbol: RD$, Code: DOP
Country Code = DZA, Price = 35,147,869.35, Symbol: د.ج.‏, Code: DZD
Country Code = ECU, Price = 35.147.869,35, Symbol: $, Code: USD
Country Code = EST, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = EGY, Price = 35,147,869.35, Symbol: ج.م.‏, Code: EGP
Country Code = ESH, Price = 35,147,869.35, Symbol: MAD, Code: MAD
Country Code = ERI, Price = 35,147,869.35, Symbol: ERN, Code: ERN
Country Code = ESP, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = ETH, Price = 35,147,869.35, Symbol: ETB, Code: ETB
Country Code = FIN, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = FJI, Price = 35,147,869.35, Symbol: FJD, Code: FJD
Country Code = FLK, Price = 35,147,869.35, Symbol: FKP, Code: FKP
Country Code = FSM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = FRO, Price = 35,147,869.35, Symbol: DKK, Code: DKK
Country Code = FRA, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = GAB, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = GBR, Price = 35,147,869.35, Symbol: £, Code: GBP
Country Code = GRD, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = GEO, Price = 35,147,869.35, Symbol: GEL, Code: GEL
Country Code = GUF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = GGY, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = GHA, Price = 35,147,869.35, Symbol: GHS, Code: GHS
Country Code = GIB, Price = 35,147,869.35, Symbol: GIP, Code: GIP
Country Code = GRL, Price = 35,147,869.35, Symbol: DKK, Code: DKK
Country Code = GMB, Price = 35,147,869.35, Symbol: GMD, Code: GMD
Country Code = GIN, Price = 35,147,869, Symbol: GNF, Code: GNF
Country Code = GLP, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = GNQ, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = GRC, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = SGS, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = GTM, Price = 35,147,869.35, Symbol: Q, Code: GTQ
Country Code = GUM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = GNB, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = GUY, Price = 35,147,869.35, Symbol: GYD, Code: GYD
Country Code = HKG, Price = 35,147,869.35, Symbol: HK$, Code: HKD
Country Code = HMD, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = HND, Price = 35,147,869.35, Symbol: L, Code: HNL
Country Code = HRV, Price = 35.147.869,35, Symbol: Kn, Code: HRK
Country Code = HTI, Price = 35,147,869.35, Symbol: HTG, Code: HTG
Country Code = HUN, Price = 35 147 869,35, Symbol: Ft, Code: HUF
Country Code = IDN, Price = 35.147.869,35, Symbol: Rp, Code: IDR
Country Code = IRL, Price = 35,147,869.35, Symbol: €, Code: EUR
Country Code = ISR, Price = 35,147,869.35, Symbol: ש"ח, Code: ILS
Country Code = IMN, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = IND, Price = 35,147,869.35, Symbol: Rs., Code: INR
Country Code = IOT, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = IRQ, Price = 35,147,869.352, Symbol: د.ع.‏, Code: IQD
Country Code = IRN, Price = 35,147,869.35, Symbol: IRR, Code: IRR
Country Code = ISL, Price = 35.147.869,, Symbol: kr., Code: ISK
Country Code = ITA, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = JEY, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = JAM, Price = 35,147,869.35, Symbol: JMD, Code: JMD
Country Code = JOR, Price = 35,147,869.352, Symbol: د.أ.‏, Code: JOD
Country Code = JPN, Price = 35,147,869, Symbol: ¥, Code: JPY
Country Code = KEN, Price = 35,147,869.35, Symbol: KES, Code: KES
Country Code = KGZ, Price = 35,147,869.35, Symbol: KGS, Code: KGS
Country Code = KHM, Price = 35,147,869.35, Symbol: KHR, Code: KHR
Country Code = KIR, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = COM, Price = 35,147,869, Symbol: KMF, Code: KMF
Country Code = KNA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = PRK, Price = 35,147,869.35, Symbol: KPW, Code: KPW
Country Code = KOR, Price = 35,147,869, Symbol: ₩, Code: KRW
Country Code = KWT, Price = 35,147,869.352, Symbol: د.ك.‏, Code: KWD
Country Code = CYM, Price = 35,147,869.35, Symbol: KYD, Code: KYD
Country Code = KAZ, Price = 35,147,869.35, Symbol: KZT, Code: KZT
Country Code = LAO, Price = 35,147,869.35, Symbol: LAK, Code: LAK
Country Code = LBN, Price = 35,147,869.35, Symbol: ل.ل.‏, Code: LBP
Country Code = LCA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = LIE, Price = 35,147,869.35, Symbol: CHF, Code: CHF
Country Code = LKA, Price = 35,147,869.35, Symbol: LKR, Code: LKR
Country Code = LBR, Price = 35,147,869.35, Symbol: LRD, Code: LRD
Country Code = LSO, Price = 35,147,869.35, Symbol: LSL, Code: LSL
Country Code = LTU, Price = 35 147 869,35, Symbol: Lt, Code: LTL
Country Code = LUX, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = LVA, Price = 35 147 869,35, Symbol: Ls, Code: LVL
Country Code = LBY, Price = 35,147,869.352, Symbol: د.ل.‏, Code: LYD
Country Code = MAR, Price = 35,147,869.35, Symbol: د.م.‏, Code: MAD
Country Code = MCO, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MDA, Price = 35,147,869.35, Symbol: MDL, Code: MDL
Country Code = MNE, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = MAF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MDG, Price = 35,147,869.35, Symbol: MGA, Code: MGA
Country Code = MHL, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = MKD, Price = 35.147.869,35, Symbol: Den, Code: MKD
Country Code = MLI, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = MMR, Price = 35,147,869.35, Symbol: MMK, Code: MMK
Country Code = MNG, Price = 35,147,869.35, Symbol: MNT, Code: MNT
Country Code = MAC, Price = 35,147,869.35, Symbol: MOP, Code: MOP
Country Code = MNP, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = MTQ, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MRT, Price = 35,147,869.35, Symbol: MRO, Code: MRO
Country Code = MSR, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = MLT, Price = 35,147,869.35, Symbol: €, Code: EUR
Country Code = MUS, Price = 35,147,869.35, Symbol: MUR, Code: MUR
Country Code = MDV, Price = 35,147,869.35, Symbol: MVR, Code: MVR
Country Code = MWI, Price = 35,147,869.35, Symbol: MWK, Code: MWK
Country Code = MEX, Price = 35,147,869.35, Symbol: $, Code: MXN
Country Code = MYS, Price = 35,147,869.35, Symbol: RM, Code: MYR
Country Code = MOZ, Price = 35,147,869.35, Symbol: MZN, Code: MZN
Country Code = NAM, Price = 35,147,869.35, Symbol: NAD, Code: NAD
Country Code = NCL, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = NER, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = NFK, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = NGA, Price = 35,147,869.35, Symbol: NGN, Code: NGN
Country Code = NIC, Price = 35,147,869.35, Symbol: $C, Code: NIO
Country Code = NLD, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = NOR, Price = 35 147 869,35, Symbol: kr, Code: NOK
Country Code = NPL, Price = 35,147,869.35, Symbol: NPR, Code: NPR
Country Code = NRU, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = NIU, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = NZL, Price = 35,147,869.35, Symbol: $, Code: NZD
Country Code = OMN, Price = 35,147,869.352, Symbol: ر.ع.‏, Code: OMR
Country Code = PAN, Price = 35,147,869.35, Symbol: B, Code: PAB
Country Code = PER, Price = 35.147.869,35, Symbol: S/, Code: PEN
Country Code = PYF, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = PNG, Price = 35,147,869.35, Symbol: PGK, Code: PGK
Country Code = PHL, Price = 35,147,869.35, Symbol: Php, Code: PHP
Country Code = PAK, Price = 35,147,869.35, Symbol: PKR, Code: PKR
Country Code = POL, Price = 35 147 869,35, Symbol: zł, Code: PLN
Country Code = SPM, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = PCN, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = PRI, Price = 35,147,869.35, Symbol: $, Code: USD
Country Code = PSE, Price = 35,147,869.35, Symbol: ILS, Code: ILS
Country Code = PRT, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = PLW, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = PRY, Price = 35.147.869, Symbol: G, Code: PYG
Country Code = QAT, Price = 35,147,869.35, Symbol: ر.ق.‏, Code: QAR
Country Code = REU, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ROU, Price = 35.147.869,35, Symbol: LEI, Code: RON
Country Code = SRB, Price = 35.147.869,35, Symbol: дин., Code: RSD
Country Code = RUS, Price = 35 147 869,35, Symbol: руб., Code: RUB
Country Code = RWA, Price = 35,147,869, Symbol: RWF, Code: RWF
Country Code = SAU, Price = 35,147,869.35, Symbol: ر.س.‏, Code: SAR
Country Code = SLB, Price = 35,147,869.35, Symbol: SBD, Code: SBD
Country Code = SYC, Price = 35,147,869.35, Symbol: SCR, Code: SCR
Country Code = SDN, Price = 35,147,869.35, Symbol: ج.س.‏, Code: SDG
Country Code = SWE, Price = 35 147 869,35, Symbol: kr, Code: SEK
Country Code = SGP, Price = 35,147,869.35, Symbol: S$, Code: SGD
Country Code = SHN, Price = 35,147,869.35, Symbol: SHP, Code: SHP
Country Code = SVN, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = SJM, Price = 35,147,869.35, Symbol: NOK, Code: NOK
Country Code = SVK, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = SLE, Price = 35,147,869.35, Symbol: SLL, Code: SLL
Country Code = SMR, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = SEN, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = SOM, Price = 35,147,869.35, Symbol: SOS, Code: SOS
Country Code = SUR, Price = 35,147,869.35, Symbol: SRD, Code: SRD
Country Code = STP, Price = 35,147,869.35, Symbol: STD, Code: STD
Country Code = SLV, Price = 35,147,869.35, Symbol: C, Code: SVC
Country Code = SXM, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = SYR, Price = 35,147,869.35, Symbol: ل.س.‏, Code: SYP
Country Code = SWZ, Price = 35,147,869.35, Symbol: SZL, Code: SZL
Country Code = TCA, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = TCD, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = ATF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = TGO, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = THA, Price = 35,147,869.35, Symbol: ฿, Code: THB
Country Code = TJK, Price = 35,147,869.35, Symbol: TJS, Code: TJS
Country Code = TKL, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = TLS, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = TKM, Price = 35,147,869.35, Symbol: TMT, Code: TMT
Country Code = TUN, Price = 35,147,869.352, Symbol: د.ت.‏, Code: TND
Country Code = TON, Price = 35,147,869.35, Symbol: TOP, Code: TOP
Country Code = TUR, Price = 35.147.869,35, Symbol: TL, Code: TRY
Country Code = TTO, Price = 35,147,869.35, Symbol: TTD, Code: TTD
Country Code = TUV, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = TWN, Price = 35,147,869.35, Symbol: NT$, Code: TWD
Country Code = TZA, Price = 35,147,869.35, Symbol: TZS, Code: TZS
Country Code = UKR, Price = 35 147 869,35, Symbol: грн., Code: UAH
Country Code = UGA, Price = 35,147,869.35, Symbol: UGX, Code: UGX
Country Code = UMI, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = USA, Price = 35,147,869.35, Symbol: $, Code: USD
Country Code = URY, Price = 35.147.869,35, Symbol: NU$, Code: UYU
Country Code = UZB, Price = 35,147,869.35, Symbol: UZS, Code: UZS
Country Code = VAT, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = VCT, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = VEN, Price = 35.147.869,35, Symbol: Bs.F., Code: VEF
Country Code = VGB, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = VIR, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = VNM, Price = 35.147.869,35, Symbol: đ, Code: VND
Country Code = VUT, Price = 35,147,869, Symbol: VUV, Code: VUV
Country Code = WLF, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = WSM, Price = 35,147,869.35, Symbol: WST, Code: WST
Country Code = YEM, Price = 35,147,869.35, Symbol: ر.ي.‏, Code: YER
Country Code = MYT, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ZAF, Price = 35,147,869.35, Symbol: R, Code: ZAR
Country Code = ZMB, Price = 35,147,869.35, Symbol: ZMK, Code: ZMK
Country Code = ZWE, Price = 35,147,869.35, Symbol: ZWL, Code: ZWL
nonSupportedLocale : 155
supportedLocale : 94

Wednesday, November 20, 2013

java convert number to word (text)

/**
 *
 * @author Pritom K Mondal
 */
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class NumberToWords {
    private static final String[] tensNames = {
        "", " ten", " twenty", " thirty", " forty", " fifty", " sixty",
        " seventy", " eighty", " ninety"
    };

    private static final String[] numNames = {
        "", " one", " two", " three", " four", " five", " six", " seven", " eight",
        " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen",
        " sixteen", " seventeen", " eighteen", " nineteen"
    };

    private static String convertLessThanOneThousand(int number) {
        String soFar;
        
        if (number % 100 < 20){
            soFar = numNames[number % 100];
            number /= 100;
        } else {
            soFar = numNames[number % 10];
            number /= 10;
            
            soFar = tensNames[number % 10] + soFar;
            number /= 10;
        }
        if (number == 0) return soFar;
        return numNames[number] + " hundred" + soFar;
    }

    private static String convert(long number) {
        // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }
        
        String snumber = Long.toString(number);
        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);
        // XXXnnnnnnnnn 
        int billions = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int millions  = Integer.parseInt(snumber.substring(3,6)); 
        // nnnnnnXXXnnn
        int hundredThousands = Integer.parseInt(snumber.substring(6,9)); 
        // nnnnnnnnnXXX
        int thousands = Integer.parseInt(snumber.substring(9,12));    
        
        String tradBillions;
        switch (billions) {
            case 0:
                tradBillions = "";
                break;
            case 1 :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
                break;
            default :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
        }
        String result =  tradBillions;
        
        String tradMillions;
        switch (millions) {
            case 0:
                tradMillions = "";
                break;
            case 1 :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
                break;
            default :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
        }
        result =  result + tradMillions;
        
        String tradHundredThousands;
        switch (hundredThousands) {
            case 0:
                tradHundredThousands = "";
                break;
            case 1 :
                tradHundredThousands = "one thousand ";
                break;
            default :
                tradHundredThousands = convertLessThanOneThousand(hundredThousands)  + " thousand ";
        }
        result =  result + tradHundredThousands;
        
        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result =  result + tradThousand;
        
        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
    }
    
    public static String convert(String number, String weight) {
        String[] numbers = number.split("\\.");
        String[] weights = weight.split("\\.");
        String word = "";
        for(Integer position = 0; position < numbers.length; position++) {
            if(numbers[position].trim().length() > 0) {
                if(position == 1) {
                    word = word.trim();
                    if(word.startsWith("zero")) {
                        word = "";
                    } else {
                        word = word.trim() + " and ";
                    }
                }
                word += NumberToWords.convert(new Long("" + numbers[position]));
                if(weights.length >= position) {
                    word = word.trim() + " " + weights[position];
                }
            }
        }
        return word.trim().toUpperCase();
    }
    
    /**
     * testing
     * @param args
     */
    public static void main(String[] args) {
        List numberList = new ArrayList<String>();
        numberList.add(0);
        numberList.add(1);
        numberList.add(7);
        numberList.add(15.98);
        numberList.add("36.06");
        numberList.add(48);
        numberList.add(67.986);
        numberList.add(89.09);
        numberList.add(435);
        numberList.add(.110);
        numberList.add(328.64);
        numberList.add(99999);
        numberList.add(100000);
        numberList.add(1000010);
        numberList.add(1002);
        numberList.add(3567);
        numberList.add(6942);
        numberList.add(9073);
        numberList.add(4583);
        numberList.add(43768);
        numberList.add(35789);
        numberList.add(35483);
        numberList.add(468368);
        numberList.add(9073254);
        numberList.add(73859735);
        numberList.add("5890203983");
        numberList.add(3000000010L);
        
        Object[] objects = numberList.toArray();
        for(Integer position = 0; position < objects.length; position++) {
            System.out.println("" + 
                    NumberToWords.convert("" + objects[position], "DOLLAR.CENT") 
                    + " (" + objects[position] + ")");
        }
    }
}

Output:

ZERO DOLLAR (0)
ONE DOLLAR (1)
SEVEN DOLLAR (7)
FIFTEEN DOLLAR AND NINETY EIGHT CENT (15.98)
THIRTY SIX DOLLAR AND SIX CENT (36.06)
FORTY EIGHT DOLLAR (48)
SIXTY SEVEN DOLLAR AND NINE HUNDRED EIGHTY SIX CENT (67.986)
EIGHTY NINE DOLLAR AND NINE CENT (89.09)
FOUR HUNDRED THIRTY FIVE DOLLAR (435)
ELEVEN CENT (0.11)
THREE HUNDRED TWENTY EIGHT DOLLAR AND SIXTY FOUR CENT (328.64)
NINETY NINE THOUSAND NINE HUNDRED NINETY NINE DOLLAR (99999)
ONE HUNDRED THOUSAND DOLLAR (100000)
ONE MILLION TEN DOLLAR (1000010)
ONE THOUSAND TWO DOLLAR (1002)
THREE THOUSAND FIVE HUNDRED SIXTY SEVEN DOLLAR (3567)
SIX THOUSAND NINE HUNDRED FORTY TWO DOLLAR (6942)
NINE THOUSAND SEVENTY THREE DOLLAR (9073)
FOUR THOUSAND FIVE HUNDRED EIGHTY THREE DOLLAR (4583)
FORTY THREE THOUSAND SEVEN HUNDRED SIXTY EIGHT DOLLAR (43768)
THIRTY FIVE THOUSAND SEVEN HUNDRED EIGHTY NINE DOLLAR (35789)
THIRTY FIVE THOUSAND FOUR HUNDRED EIGHTY THREE DOLLAR (35483)
FOUR HUNDRED SIXTY EIGHT THOUSAND THREE HUNDRED SIXTY EIGHT DOLLAR (468368)
NINE MILLION SEVENTY THREE THOUSAND TWO HUNDRED FIFTY FOUR DOLLAR (9073254)
SEVENTY THREE MILLION EIGHT HUNDRED FIFTY NINE THOUSAND SEVEN HUNDRED THIRTY FIVE DOLLAR (73859735)
FIVE BILLION EIGHT HUNDRED NINETY MILLION TWO HUNDRED THREE THOUSAND NINE HUNDRED EIGHTY THREE DOLLAR (5890203983)
THREE BILLION TEN DOLLAR (3000000010)